填充文本框和下拉列表

时间:2016-06-15 17:20:43

标签: vb.net gridview drop-down-menu webforms

我很难尝试填充文本框和两个下拉列表。我试图做的是:当选择公司下拉列表时,它将显示一个sqldatasource的相应值的网格。现在下一步是从公司下拉列表中选择一个,文本框将显示membertype以及第二个下拉列表。第三个下拉列表显示groupid。选择完成后,所有这三个组件都有其输出。但我似乎无法得到显示的价值观。我还添加了代码以便澄清。

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Partial Class companydropdown
Inherits System.Web.UI.Page

Private Sub BindDropDownList(DropdownList1 As DropDownList, query As String, text As String, value As String, defaultText As String)
    If Not IsPostBack Then
        ' Read sql server connection string from web.config file
        Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
        Dim Conn As New SqlConnection(sConstr)
        Dim dt As New DataTable("tbl")

        Using Conn
            Conn.Open()
            Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList ORDER BY CompanyName", Conn)
            Dim da As New SqlDataAdapter(comm)
            'da.Fill(dt)
        End Using

        'DropdownList1.DataSource = dt
        DropdownList1.DataTextField = "CompanyName"
        DropdownList1.DataValueField = "CompanyID"
        DropdownList1.DataBind()
        'DropdownList1.Items.Insert(0, New ListItem("--Select--"))
    End If
End Sub

Protected Sub Member_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedIndex > 0 Then
        Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
        Dim Conn As New SqlConnection(sConstr)
        Dim dt As New DataTable("tbl")

        Using Conn
            Conn.Open()
            Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList " + DropDownList1.SelectedValue & " ORDER BY CompanyName", Conn)
            Dim da As New SqlDataAdapter(comm)
            da.Fill(dt)
        End Using

        'DropDownList2.DataSource = dt
        DropDownList2.DataTextField = "MembershipStatus"
        DropDownList2.DataValueField = "MemberTypeID"
        ' Bind sql server data into the Dropdown List
        DropDownList2.DataBind()
    Else
        DropDownList2.Items.Clear()
    End If

End Sub

Protected Sub Group_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedIndex > 0 Then
        Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
        Dim Conn As New SqlConnection(sConstr)
        Dim dt As New DataTable("tbl")

        Using Conn
            Conn.Open()
            Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList " + DropDownList1.SelectedValue & " ORDER BY CompanyName", Conn)
            Dim da As New SqlDataAdapter(comm)
            da.Fill(dt)
        End Using

        'DropDownList2.DataSource = dt
        DropDownList3.DataTextField = "GroupID"
        DropDownList3.DataValueField = "GroupID"
        ' Bind sql server data into the Dropdown List
        DropDownList3.DataBind()
    Else
        DropDownList3.Items.Clear()
    End If

End Sub



Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
    DropDownList2.Enabled = False
    'DropDownList3.Items.Clear()
    DropDownList3.Items.Insert(0, New ListItem("", "0"))
    Dim stateId As Integer = Integer.Parse(DropDownList1.SelectedItem.Value)
    If stateId > 0 Then
        Dim query As String = String.Format("", stateId)
        BindDropDownList(DropDownList1, query, "CompanyName", "CompanyId", "")
        DropDownList3.Enabled = True
        DropDownList2.Enabled = True
    End If
End Sub


End Class

Populating Textbox and dropdowns

0 个答案:

没有答案