长下拉列表值 [英] Long Dropdownlist values

查看:53
本文介绍了长下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含30个菜单的下拉列表。我必须根据下拉列表中的选项选中或取消选中一个复选框。



目前我在下面做方法:

 如果(ddlbselect.selectedvalue ==   选项1
Chktest.checked = True
解决方案

很多方法这样做,取决于你如何设置。您可能希望利用以下事实:您可以在下拉列表项中存储两条信息。这是一种方式:



 受保护的  Sub  Page_Load( ByVal  sender  As  对象 ByVal  e  As  System.EventArgs)句柄  .Load 
如果 IsPostBack 然后
Dim dt 作为 DataTable
dt.Columns.Add( Text
dt.Columns.Add( 数据
Dim dr As DataRow = dt.NewRow
dt.Rows.Add( New 对象(){ Nothing Nothing })
dt.Rows.Add ( 对象(){ < span class =code-string> odd, Checkbox1~Checkbox3~Checkbox5})
dt.Rows.Add( New Object (){ even Checkbox2~Checkbox4})

DropDownList1.DataValueField = 数据
DropDownList1.DataTextField = Text
DropDownList1.DataSource = dt
DropDownList1.DataBind()
End < span class =code-keyword>如果
结束 Sub

私有 Sub DropDownList1_SelectedIndexChanged(sender As 对象,e As System.EventArgs)句柄 DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedValue.ToString.Length> 0 然后
Dim chks () As String = DropDownList1.SelectedValue.ToString.Split(
对于 i 作为 整数 = 0 chks.Length - 1
DirectCast .FindControl(chks(i)),CheckBox).Checked = True
下一步
其他
对于 i As 整数 = 1 5
DirectCast Me .FindControl( 复选框& i),CheckBox).Checked = False
下一步
结束 如果

结束 < span class =code-keyword> Sub


I have a dropdownlist with 30 menus in it .I have to check or uncheck a checkbox as per the selections from dropdown.

Presently i am doing in below approach :

If (ddlbselect.selectedvalue == "Option 1")
Chktest.checked = True

Please let me know if we have any alternate approach for doing the same , i want to avoid this hardcoding as the list is long.

解决方案

Lots of ways of doing this, depending on how you set things up. You're likely going to want to take advantage of the fact that you can store two pieces of information in your dropdownlist items. Here's one way:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As New DataTable
        dt.Columns.Add("Text")
        dt.Columns.Add("Data")
        Dim dr As DataRow = dt.NewRow
        dt.Rows.Add(New Object() {Nothing, Nothing})
        dt.Rows.Add(New Object() {"odd", "Checkbox1~Checkbox3~Checkbox5"})
        dt.Rows.Add(New Object() {"even", "Checkbox2~Checkbox4"})

        DropDownList1.DataValueField = "Data"
        DropDownList1.DataTextField = "Text"
        DropDownList1.DataSource = dt
        DropDownList1.DataBind()
    End If
End Sub

Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedValue.ToString.Length > 0 Then
        Dim chks() As String = DropDownList1.SelectedValue.ToString.Split("~")
        For i As Integer = 0 To chks.Length - 1
            DirectCast(Me.FindControl(chks(i)), CheckBox).Checked = True
        Next
    Else
        For i As Integer = 1 To 5
            DirectCast(Me.FindControl("Checkbox" & i), CheckBox).Checked = False
        Next
    End If

End Sub


这篇关于长下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆