GridView中的CheckBox获取值

时间:2014-09-18 08:18:30

标签: vb.net gridview checkbox

我有以下代码

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>

代码背后:

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

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

的输出
Debug.WriteLine(chk.Checked)
Debug.WriteLine(lbl.Text)

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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