Alexander Ryan Baggett
Alexander Ryan Baggett

Reputation: 2397

How to operate items in an itemtemplate by ID

Now I looked at the MSDN on ItemTemplates, but I didn't see how to access them by ID.

Here's a link http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.itemtemplate.aspx

I thought it would be straight forward just the same as accessing any other control in the codebehind or in a server script, but it doesn't work. I keep getting the "Does not exist in the current context" error when I try to refer to it by ID.

What I am trying to do is access a header checbox's checked property and use that to select or deselect all of the checkboxes in the ItemTemplate. I will also need whether they are selected or not later on for other uses in my code.

Here is the code for the gridview I am using in my project.

<asp:GridView ID="ApplicationsGridView" runat="server"
   AutoGenerateColumns="True"
   visible="true"
   Font-Size="Smaller"
   CellPadding="5"
   Width="1200px"
   BorderStyle="Solid"
   BorderColor="Black"
   OnDataBinding="ApplicationsGridView_DataBinding">
<%-- Add the checkboxes declaratively  --%>
<Columns>
  <asp:TemplateField>
    <HeaderTemplate>
      <asp:CheckBox runat="server" ID="checkall" Checked="true" OnCheckedChanged="checkall_CheckedChanged" />
      <script runat="server">
        protected void checkall_CheckedChanged(object sender, EventArgs e)
        {
          if(checkall.checked)
          {
            foreach (GridViewRow row in ApplicationsGridView.Rows { }
          }         
        }
      </script>
    </HeaderTemplate>
    <ItemTemplate>
      <asp:CheckBox runat="server" ID="checkboxes" Checked="true" />
    </ItemTemplate>
  </asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#d2d2f2" />
<HeaderStyle Font-Bold="true" BackColor="#052a9f"  ForeColor="#eeeeff"  Font-Size="Medium"/>
</asp:GridView>

Originally, I had tried accessing the ID in the codebehind. But even trying with a server script it still cannot find it. How do I access the checkboxes if not by ID?

Edit: This works =)

    protected void checkall_CheckedChanged(object sender, EventArgs e)
    {
        //get whether its checked or not.
        CheckBox theCheckBox = sender as CheckBox;      

        //check them all if checked. Uncheck them all when unchecked.
        if (theCheckBox.Checked)
        {
            foreach (GridViewRow row in ApplicationsGridView.Rows)
            {

                CheckBox cb = row.FindControl("checkboxes") as CheckBox;
                cb.Checked = true;
            }
        }

        else if (!(theCheckBox.Checked))
        {
            foreach (GridViewRow row in ApplicationsGridView.Rows)
            {

                CheckBox cb = row.FindControl("checkboxes") as CheckBox;
                cb.Checked = false;
            }

        }
    }

Upvotes: 2

Views: 3104

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34846

You need to check the type of row when you are looping through all the rows in the grid, like this:

foreach (GridViewRow row in ApplicationsGridView.Rows)
{
    if(row.RowType == DataControlRowType.Header)
    {
        // Search for checkbox by ID here
        CheckBox theCheckBox = row.FindControl("checkall") as CheckBox;

        // Do whatever you need to do with checkbox here
    }
}

UPDATE:

You do not need to search for the actual control, because the check box initiated the event, so you can just do this:

protected void checkall_CheckedChanged(object sender, EventArgs e)
{
    // Cast the sender of the event to a check box, because the check box created this event
    CheckBox theCheckBox = sender as CheckBox;

    if (theCheckBox.Checked)
    {
        foreach (GridViewRow row in ApplicationsGridView.Rows)
        {   
            // Here is where you want to search for the existing check boxes, not create new ones
            CheckBox cb = row.FindControl("checkboxes") as CheckBox;
            cb.Checked = true;
        }
    }
}

Upvotes: 1

Hogan
Hogan

Reputation: 70523

If you want to do this on the client side (and typically you would, users expect a checkall box to check all the boxes) you will need the ClientID from the control at render time in the page lifecycle.

In pre 4.0 you could "cheat" and look at the rendered page (view source from the browser). However, this is a fragile way to do it since it might change with every edit of the .aspx page.

If you have the most recent framework (4.0 or later) you can set ClientIDMode to Static. Then you will be able to use the value in the ID attribute as the ClientID.

http://msdn.microsoft.com/en-us/library/1d04y8ss%28v=vs.100%29.aspx

Upvotes: 1

Related Questions