如何获得复选框列表的值?

本文关键字:列表 何获得 复选框 | 更新日期: 2023-09-27 18:06:12

我正在创建一个CheckBoxList并动态填充它。我遍历了代码,它似乎没有击中下面的方法内部,我认为所选的value属性的值总是false。

<form id="form1" action="web_page.aspx">
   <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
           onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" >
   </asp:CheckBoxList>
   <br />
   <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="submit"/>
</form>
<div id="p" runat="server"></div>
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.p.InnerHtml = "hello";
}

如何获得复选框列表的值?

问题可能是您绑定了page_load事件。

尝试将其绑定在page load上但在!Page.IsPostBack

 if(!Page.IsPostBack)
 {
     //bind here.
 }

您应该能够将发送方转换为复选框列表,然后遍历项目:

  Protected Sub test(ByVal sender As Object, ByVal e As EventArgs) Handles check1.SelectedIndexChanged
    Dim myCb As New CheckBoxList
    myCb = Me.check1
    For Each c As ListItem In myCb.Items
        Dim isChecked, value, text As String
        isChecked = c.Selected
        If (isChecked = True) Then
            value = c.Value
            text = c.Text
        End If
    Next
End Sub