Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / Javascript

Drag and Drop on a Web Form

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
18 Jan 2010CPOL4 min read 55.6K   1.8K   24  
How to insert a textbox on a Web page at runtime, dragging it to the right position and by using a database retrieve the position when starting the application up again
Imports System.Data.OleDb
Imports System.Data

Partial Class DragAndDrop
    Inherits System.Web.UI.Page

    Shared myCount As Integer = 0

    Dim FilePath As String = Server.MapPath("Database\Database.mdb")
    Dim conString As String = "Provider=microsoft.jet.OLEDB.4.0;" & _
                   "Data Source =" & FilePath
    'Setter opp connection og leseren
    Dim conn As New OleDbConnection(conString)

    Dim selectString As String
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            AddName.Visible = False
        End If

    End Sub


    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        conn.Open()
        selectString = "SELECT * FROM tblxy"
        cmd = New OleDbCommand(selectString, conn)
        reader = cmd.ExecuteReader()

        While reader.Read()
            Dim textBox As TextBox = New TextBox()
            textBox.ID = reader.Item("Id")
            textBox.Text = reader.Item("Name").ToString
            textBox.Style.Value = "left: " & reader.Item("xpos") & "px; position: absolute; top: " & reader.Item("ypos") & "px"
            textBox.CssClass = "drag"
            textBox.Attributes.Add("onmousedown", "whichElement(event)")
            textBox.Attributes.Add("onmouseup", "mymouseup(event)")
            myPlaceHolder.Controls.Add(textBox)
        End While
        reader.Close()
        conn.Close()

    End Sub


    Private Sub Add()

        xpos.Text = ""
        ypos.Text = ""
        id.Text = ""

        myCount += 1

        If myCount = 1 Then
            selectString = "Insert into tblxy (xpos, ypos) Values (" & _
                                                 "'" & 25 & "'," & _
                                                  "'" & 75 & "')"


            If conn.State = ConnectionState.Closed Then conn.Open()

            cmd = New OleDbCommand(selectString, conn)
            cmd.ExecuteNonQuery()
            If conn.State = ConnectionState.Open Then conn.Close()


            xpos.Text = "25"
            ypos.Text = "75"
            id.Text = FindLastId()

        Else
            myCount = 0
        End If

    End Sub

    Private Function FindLastId() As String

        selectString = "Select max(id) FROM tblxy"
        If conn.State = ConnectionState.Closed Then conn.Open()

        Dim cmd = New OleDbCommand(selectString, conn)

        Dim Id As String
        Id = cmd.ExecuteScalar()

        If conn.State = ConnectionState.Open Then conn.Close()

        Return Id
    End Function


    Private Sub Delete()
        If id.Text.Length > 0 Then

            Dim MYid() As String = Split(id.Text)

            For i As Integer = 0 To MYid.Length - 1
                If MYid(i) <> "" Then

                    If conn.State = ConnectionState.Closed Then conn.Open()
                    selectString = "DELETE FROM tblxy WHERE(Id =" & MYid(i) & ")"
                    cmd = New OleDbCommand(selectString, conn)
                    reader = cmd.ExecuteReader()
                    reader.Close()
                    If conn.State = ConnectionState.Open Then conn.Close()

                    xpos.Text = ""
                    ypos.Text = ""
                    id.Text = ""

                End If
            Next
        End If

    End Sub

    Private Sub Update()

        If xpos.Text.Length > 0 And ypos.Text.Length > 0 And id.Text.Length > 0 Then
            If AddName.Text.Length > 0 Then

                selectString = "Update (tblxy) Set " & _
                                          "xpos=" & "'" & xpos.Text & "'," & _
                                          "ypos=" & "'" & ypos.Text & "'," & _
                                           "Name=" & "'" & AddName.Text & "'" & _
                                          " WHERE Id=" & id.Text & ""

                If conn.State = ConnectionState.Closed Then conn.Open()

                cmd = New OleDbCommand(selectString, conn)
                cmd.ExecuteNonQuery()
                If conn.State = ConnectionState.Open Then conn.Close()

                xpos.Text = ""
                ypos.Text = ""
                id.Text = ""
                AddName.Text = ""
                AddName.Visible = False

            Else

                Dim MYxpos() As String = Split(xpos.Text)
                Dim MYypos() As String = Split(ypos.Text)
                Dim MYid() As String = Split(id.Text)


                For i As Integer = 0 To MYid.Length - 1
                    If MYid(i) <> "" Then


                        selectString = "Update (tblxy) Set " & _
                                                                  "xpos=" & "'" & MYxpos(i) & "'," & _
                                                                  "ypos=" & "'" & MYypos(i) & "'" & _
                                                                  " WHERE Id=" & MYid(i) & ""

                        If conn.State = ConnectionState.Closed Then conn.Open()

                        cmd = New OleDbCommand(selectString, conn)
                        cmd.ExecuteNonQuery()
                        If conn.State = ConnectionState.Open Then conn.Close()
                    End If
                Next

                xpos.Text = ""
                ypos.Text = ""
                id.Text = ""
            End If


        End If

    End Sub

    Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
        If RadioButtonList1.SelectedIndex = 0 Then 'Normal


        ElseIf RadioButtonList1.SelectedIndex = 1 Then 'Update

            Update()
            RadioButtonList1.SelectedIndex = 0
            AddName.Visible = False
            AddName.Text = ""

        ElseIf RadioButtonList1.SelectedIndex = 2 Then 'Add
            Add()
            AddName.Visible = True
            RadioButtonList1.SelectedIndex = 0

        ElseIf RadioButtonList1.SelectedIndex = 3 Then 'Delete
            Delete()
            RadioButtonList1.SelectedIndex = 0

        End If

    End Sub


End Class


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions