vb.net GridView 中的复选框获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25907569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:13:53  来源:igfitidea点击:

CheckBox in GridView get value

vb.netgridviewcheckbox

提问by Z.V

I have the following code

我有以下代码

HTML:

HTML:

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="Checkbox">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkb" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="ID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id")%>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Country">
                        <ItemTemplate>
                            <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>

Code behind:

后面的代码:

Protected Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        Debug.WriteLine("Clicked")
        For Each row As GridViewRow In GridView1.Rows
            Dim chk As CheckBox = row.FindControl("chkb")
            If chk.Checked Then
                Dim lbl As Label = row.FindControl("lblId")
                Debug.WriteLine(chk.Checked)
                Debug.WriteLine(lbl.Text)
            End If
        Next
    End Sub

Is there something wrong around the checkbox section? because I can get the ouput "Clicked" but can't seem to get the output for

复选框部分有问题吗?因为我可以获得输出“点击”但似乎无法获得输出

Debug.WriteLine(chk.Checked)
Debug.WriteLine(lbl.Text)

Also, I cannot get the value whether the chk.Checkedwhen I debug.

此外,我无法chk.Checked在调试时获得该值。

回答by Thirisangu Ramanathan

Try this:

尝试这个:

 If Not Page.IsPostBack Then
 GridView1.DataSourceID = dataTable
 GridView1.DataBind()
 End If