【问题标题】:Add conditional dropdownlist items in ASPX Application (VB)在 ASPX 应用程序 (VB) 中添加条件下拉列表项
【发布时间】:2018-09-12 03:07:35
【问题描述】:

我有一个带有 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

非常感谢任何帮助。

【问题讨论】:

    标签: asp.net vb.net drop-down-menu conditional


    【解决方案1】:

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

    编辑:我刚刚意识到您正在循环行。您不必这样做 - 这是 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
    

    【讨论】:

    • 感谢 Wazz,这让我得到了答案。您混合了 C# 和 VB。我对您的代码添加了编辑并标记为已回答
    猜你喜欢
    • 2018-12-20
    • 2015-06-24
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2021-05-05
    • 2020-04-21
    相关资源
    最近更新 更多