How to get DropDownList to copy data from a TextBox?

110 Views Asked by At

Currently in my Web form I have a cascading drop down list, but I'm focusing on the 1st drop down list for now.

Here is the code to load the list.

Private Sub FillLine() ' load 1st dropdown data

    Dim dt As New DataTable
    Dim strSql = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"
    Using conn As New SqlConnection(ConStr),
        cmd As New SqlCommand(strSql, conn)
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count > 0 Then
        line.DataSource = dt
        line.DataTextField = "LCODE1" 
        line.DataValueField = "level1_id"

        line.DataBind()
        line.Items.Insert(0, "")
    End If

End Sub

And here is the visual representation of how the data I'm pulling looks like.

| level1_id |    LCODE1     |
-----------------------------
|     1     |  A01 | Line 1 |

I also want to have the drop down list auto load in the correct value based on URL query string.

My plan is to 1st load the value in an invisible textbox, and instruct drop down list to select the same data based on that textbox using page load event.

'load textbox with query string info

Sub loadFrmQuery()

    Dim LID As String = Request.QueryString("LID")

    Dim cmd As New SqlCommand("SELECT level1_id " _
                            & ", level2_id " _
                            & ", level3_id " _
                            & "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] " _
                            & "where [LEVEL3_ID] = '" & LID & "'", conn)

    conn.Open()

    Dim rdr As SqlDataReader = cmd.ExecuteReader

    While rdr.Read

        line_text.Text = rdr("level1_id")

        process_text.Text = rdr("level2_id")

        equip_text.Text = rdr("level3_id")

    End While

    conn.Close()

End Sub

'page load event

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Session("EmpID") Is Nothing Then

        Response.Redirect("~/Login.aspx")

    Else

        If Not IsPostBack Then 'whenever hv dropdownlist, need to have this in load sub, or else won't get ddl data.

            FillLine()
            loadFrmQuery()

            line.DataValueField = line_text.Text

        End If

    End If

End Sub

But during testing, the drop down list won't load in data. I tried calling out the DataTextField and DataValueField of the drop down list and still no response. What should I do in this case?

0

There are 0 best solutions below