在 ASPX 应用程序中添加条件下拉列表项 (VB)

Add conditional dropdownlist items in ASPX Application (VB)

我有一个带有 asp:dropdownlist

的 GridView

我可以使用 rowdatabound 添​​加下拉值。

但是我需要做的是根据该行中另一个单元格的内容添加特定的下拉列表值。

例如,如果 Row(1) 和 Cell(2) = MAR001,我需要下拉列表值四、五和六

但是,如果 Row(1) 和 Cell(2) <> MAR001,我需要下拉值一、二和三。

这是我使用循环的尝试,但它没有正确定位下拉值。

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        For i As Integer = 0 To GridView1.Rows.Count - 1

            Dim Name As String = GridView1.Rows(i).Cells(2).Text

            If Name = "MAR001" Then
                Dim ddl As DropDownList = e.Row.FindControl("ddlQuantity")
                Dim numbers As New List(Of String)()
                numbers.Add("Four")
                numbers.Add("Five ")
                numbers.Add("Six")
                ddl.DataSource = numbers
                ddl.DataBind()


            Else
                Dim ddl As DropDownList = e.Row.FindControl("ddlQuantity")
                Dim numbers As New List(Of String)()
                numbers.Add("One")
                numbers.Add("Two")
                numbers.Add("Three")
                ddl.DataSource = numbers
                ddl.DataBind()
            End If
        Next
    Else
    End If
End Sub

非常感谢任何帮助。

不确定这是否是您需要的,但请先尝试找到文本框并获取它的文本值。

编辑: 我刚刚意识到你在循环行。您不必这样做 - 这是 RowDataBound 事件,因此每一行都会出现此代码。

If e.Row.RowType = DataControlRowType.DataRow Then

     Dim ddl As DropDownList = e.Row.FindControl("ddlQuantity")
     Dim numbers As New List(Of String)()

     Dim customerid As String = (e.Row.Cells(2).Text)

     If customerid = "MAR001" Then
         numbers.Add("Four")
         numbers.Add("Five ")
         numbers.Add("Six")
     Else
         numbers.Add("One")
         numbers.Add("Two")
         numbers.Add("Three")
     End If

     ddl.DataSource = numbers
     ddl.DataBind()
 End If