views:

485

answers:

4

Hi All

Not really sure how to handle this issue but here goes...

I have a gridview with two checkboxes for each row, below is an example of the item template:

<ItemTemplate>
    <asp:CheckBox ID="MasterCheckbox" runat="server"/>
    <asp:CheckBox ID="ChildCheckbox" runat="server" />   
</ItemTemplate>

I would like the 'enabled' property of the ChildCheckbox to be controlled by the 'Checked' property of the MasterCheckbox ... so in other words the ChildCheckbox is only enabled when the MasterCheckbox has been checked.

I know that I will need to append a handler onto the MasterCheckbox control to invoke some javascript to perform the necessary actions on the client-side - this will probably be done in the row_databound() method?

I can't quite figure out the javascript required to get this to work, so any hints/tips would be welcome.

Thanks

Dal

A: 

any suggestions?

Dal
A: 

Ok well since there are no answers for a pure javascript solution, does anyone know how I would handle the 'autopostback' when inside a GridViewRow... this will allow me to alter the second checkbox serverside and re-render the gridviewrow... not sure how to handle this when the control is located inside a GridViewRow?

Any thoughts?

Dal
+2  A: 

First you dont need to answer your own question,you can add comments into your very first question.

Since you are using GridView , I think you are binding something for MasterCheckBox, so let's say that it's boolean value in a dataTable. For Example it's a row contaning column with name IsMasterChecked

You can handle Enabling the other one with binding custom expressions as

<ItemTemplate>
   <asp:CheckBox ID="MasterCheckbox" runat="server" />
   <asp:CheckBox ID="ChildCheckbox" runat="server" Enabled='<%# Convert.ToBoolean(Eval("IsMasterChecked")) %>'/>   
</ItemTemplate>

or

    <ItemTemplate>
   <asp:CheckBox ID="MasterCheckbox" runat="server" />
   <asp:CheckBox ID="ChildCheckbox" runat="server" Enabled='<%# Convert.ToBoolean(Eval("IsMasterChecked")) ? "true" : "false" %>'/>   
</ItemTemplate>

Hope this helps.

Myra
Thanks Myra - That should do it... does the post move towards the top even if you add a comment? Ta.
Dal
Mostly voted posts go top :)
Myra
A: 

Off the top of my head, I think what you will have to do is something along the lines of the following...

  <asp:TemplateField HeaderText="Checkbox">
   <ItemTemplate>
   <asp:CheckBox ID="MasterCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkGridViewChkBox" />
   </ItemTemplate>
  </asp:TemplateField>

With the following code behind.

CheckBox MasterCheckbox;
CheckBox ChildCheckbox;

private void checkGridViewChkBox()
{
    int i;
    int x = GridView1.Rows.Count;

    for (i = 0; i < x; i++)   //loop through rows
    {
        findControls(i);

        if (MasterCheckbox.Checked)
        {
           ChildCheckbox.Enabled = true;
        }else{   
  ChildCheckbox.Enabled = false;  
  }      
    }

}

private void findControls(int i)
{                                                               
    MasterCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("MasterCheckbox"));
    ChildCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("ChildCheckbox")); 
}

It's not terribly efficient but works ok.

Steve McCall