Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何选中复选框列表中的选定项(由代码生成)_Asp.net_Vb.net_Checkboxlist - Fatal编程技术网

Asp.net 如何选中复选框列表中的选定项(由代码生成)

Asp.net 如何选中复选框列表中的选定项(由代码生成),asp.net,vb.net,checkboxlist,Asp.net,Vb.net,Checkboxlist,我有一个ASP.NET/VB应用程序 我需要选中复选框列表中的选定项,但复选框是从代码隐藏动态生成的 <asp:Panel ID="panel1" runat="server"> <asp:Button ID="Button1" runat="server" Text="Save" /> <asp:Label ID="lbl_Selected_Items" runat="server" Text=""></asp:Label> &l

我有一个ASP.NET/VB应用程序 我需要选中复选框列表中的选定项,但复选框是从代码隐藏动态生成的

<asp:Panel ID="panel1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Save" />
    <asp:Label ID="lbl_Selected_Items" runat="server" Text=""></asp:Label>
</asp:Panel>
按钮1单击事件

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CheckBoxList1 As CheckBoxList = CType(Me.FindControl("CheckBoxList"), CheckBoxList)
Dim sCheckedValue As String = ""
For Each oItem As ListItem In CheckBoxList1.Items
    If oItem.Selected Then
        If sCheckedValue = "" Then
            sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
        Else
            sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
        End If
    End If
Next
lbl_Selected_Items.Text = sCheckedValue
Protected Sub Button1\u Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理按钮1。单击
Dim CheckBoxList1作为CheckBoxList=CType(Me.FindControl(“CheckBoxList”),CheckBoxList)
Dim sCheckedValue As String=“”
对于CheckBoxList1.Items中的每个oItem作为ListItem
如果选择了oItem,则
如果sCheckedValue=“”,则
sCheckedValue=(“选定值:”+oItem.Value&“选定文本:”)+oItem.Text
其他的
sCheckedValue+=(“
所选值:”+oItem.Value&“所选文本:”)+oItem.Text 如果结束 如果结束 下一个 lbl_Selected_Items.Text=sCheckedValue
错误文本:空引用异常对象引用未设置为对象的实例-
那么发生了什么?错误?没有结果

此时,我唯一可以推荐的是在pageinit中加载动态控件,而不是在pageload中加载

编辑

调试时,您会发现按钮单击第一行上的复选框为空。代码中有两个错误

  • 将控件添加到表单时,不提供id值。并且不查找此值

  • 在按钮中查找控件时,单击FindControl仅在单个容器中查找。它不是递归的。您应该调用pagerView.FindControl

  • 页面加载(我仍然建议使用init):

    然后单击按钮:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CheckBoxList1 As CheckBoxList = CType(pagerview.FindControl("cbl1"), CheckBoxList)
        Dim sCheckedValue As String = ""
        For Each oItem As ListItem In CheckBoxList1.Items
            If oItem.Selected Then
                If sCheckedValue = "" Then
                    sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                Else
                    sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                End If
            End If
        Next
        lbl_Selected_Items.Text = sCheckedValue
    End Sub
    
    Protected Sub Button1\u Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理按钮1。单击
    Dim CheckBoxList1作为CheckBoxList=CType(pagerview.FindControl(“cbl1”),CheckBoxList)
    Dim sCheckedValue As String=“”
    对于CheckBoxList1.Items中的每个oItem作为ListItem
    如果选择了oItem,则
    如果sCheckedValue=“”,则
    sCheckedValue=(“选定值:”+oItem.Value&“选定文本:”)+oItem.Text
    其他的
    sCheckedValue+=(“
    所选值:”+oItem.Value&“所选文本:”)+oItem.Text 如果结束 如果结束 下一个 lbl_Selected_Items.Text=sCheckedValue 端接头

    谢谢,错误为空引用异常对象引用未设置为object@MrSmith这样的事情必须出现在评论中,而不是答案中!答案是确切的或可能的解决方案,而您的问题或建议是comment@Khazratbek谢谢你说清楚。但是否决票,你今天过得不好吗,伙计?享受答案。@Ibrahim Alsurkhi这解决了你的问题吗?如果是的话,你能标出答案吗?
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Handles Me.Load
        Dim CheckBoxList As New CheckBoxList
        CheckBoxList.ID = "cbl1"
        pagerview.Controls.Add(CheckBoxList)
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CheckBoxList1 As CheckBoxList = CType(pagerview.FindControl("cbl1"), CheckBoxList)
        Dim sCheckedValue As String = ""
        For Each oItem As ListItem In CheckBoxList1.Items
            If oItem.Selected Then
                If sCheckedValue = "" Then
                    sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                Else
                    sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                End If
            End If
        Next
        lbl_Selected_Items.Text = sCheckedValue
    End Sub