views:

27

answers:

1

Dear Friends,

I have a grid view control with Template Field containing Item Template as Checkbox control and the Header Template is containing the label with column header name.

I want to click the coulmn header label and all the check boxes must be checked once.

Please provide me some examples or ideas how i can achieve this

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4"
        ForeColor="#333333" GridLines="None" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField HeaderText="" >
            <ItemTemplate>
            <asp:CheckBox ID="val_id" runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
            <label>
            Rise Needed
            </label>
            </HeaderTemplate>
            </asp:TemplateField>
        <Columns>

A: 

Change your HeaderTemplate label to be a LinkButton and assign the Click event.

        <HeaderTemplate>       
        <asp:LinkButton ID="btnRiseNeeded" runat="server" Text="Rise Needed" OnClick="btnRiseNeeded_Click" />       
        </HeaderTemplate>  

Then when the button is clicked loop through you GridView rows and check the box.

foreach(var row in GridView2.Rows)
{
   var cbx = (CheckBox)row.FindControl("val_id");
   cbx.Checked = true;
}

This code is off the top of my head so might need some modifying. Also, I'm not sure if the checkboxes will stay checked on PostBack. Give it a try.

Jamie