在Grid View内部模板中获取CheckBox组件

时间:2013-11-29 03:03:22

标签: asp.net vb.net gridview

我有这个aspx片段:

<asp:updatepanel runat="server" id="resultPanel">
    <contenttemplate>
            <app:exgridview id="referenceGridView" runat="server" allowpaging="True" allowsorting="True">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="checkAllBox" runat="server" CausesValidation="false"></asp:CheckBox> 
                </HeaderTemplate>
<asp:TemplateField>
.....

如何在checkAllBox代码中获取.vb组件?

我试过这个:

referenceGridView.HeaderRow _ .Cells(0).FindControl("checkAllBox")

并且

referenceGridView.FindControl("checkAllBox")

但它也不起作用。

2 个答案:

答案 0 :(得分:0)

<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound:

 void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
 {
   Label l = (Label) e.Row.FindControl("lblName");
 }

答案 1 :(得分:0)

EDITED

您可以在GridView.HeaderRow中找到该控件:

注意:此代码块应在referenceGridView.DataBind()之后执行。我在按下按钮时运行它:

Protected Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click

Dim checkAllBox As CheckBox = TryCast(referenceGridView.HeaderRow.FindControl("checkAllBox"), CheckBox)

If checkAllBox IsNot Nothing Then
    'checkAllBox exists here.
    'Place your code for checkAllBox 
    If checkAllBox.Checked Then
        lblResult.Text = "All checked"
    Else
        lblResult.Text = "All not checked"
    End If
End If
End Sub

假设您已在页面加载中使用此功能:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    'clear xml on page load if it contain data
    If Not Page.IsPostBack Then
        'Getting datasource
        'referenceGridView.DataSource = MyDataSource.GetTable()
        referenceGridView.DataBind()
    End If

End Sub

我在标记中的测试按钮:

<asp:button  id="btnTest" runat="server"  Text="TEST" />

您可以下载测试项目here