Coder 2
Coder 2

Reputation: 4881

How to access a checkBox inside a repeater Header?

I have a repeater which has a checkBox in the headerTemplate

<asp:Repeater ID="myRepeater" runat="Server">
  <HeaderTemplate>
    <table>
      <tr>
        <th>
          <asp:CheckBox ID="selectAllCheckBox" runat="Server" AutoPostBack="true>
                         .
                         .
                         .

I'm wanting to know how I can get the value of that checkBox in the code behind. Any ideas?

Upvotes: 2

Views: 3024

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83356

Inside your ItemDataBound method call FindControl("checkboxID") and cast to Checkbox

void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.Header) {
        CheckBox cb = (CheckBox)e.Item.FindControl("selectAllCheckBox");
        bool isChecked = cb.Checked;
    }
}

Or you could do this anytime:

CheckBox cb = (CheckBox)myRepeater.Controls.OfType<RepeaterItem>().Single(ri => ri.ItemType == ListItemType.Header).FindControl("selectAllCheckBox");

Upvotes: 2

Related Questions