JavaScript - How to find Nested GridView From Main Gridview using JavaScript

Asked By Anto Bilson on 16-Jul-11 02:54 AM

 hi Friends..
 

i've CheckBox Control in Nested Gridview Inside The Main GridView .. I need to find the Controls(Nested GridView and CheckBox) Ids using JavaScript..



pls Give Me solution
Riley K replied to Anto Bilson on 16-Jul-11 03:11 AM
Assuming this is your gridview

<asp:GridView runat="server" ID="grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound">
        <Columns>
          <asp:BoundField DataField="<Column Name>" HeaderText="Header Text" />
        <asp:TemplateField HeaderText="Child Grid">
          <ItemTemplate>
           <asp:GridView runat="server" ID="ChldGrid" AutoGenerateColumns="false" OnRowDataBound="OnChidDataBound">
              <Columns>
                <asp:TemplateField>
                  <ItemTemplate>
                    <asp:CheckBox runat='server' ID="chk" />
                  </ItemTemplate>
                  <HeaderTemplate>
                    <asp:CheckBox runat='server' ID="chkHeader" />
                  </HeaderTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Header Text" DataField="<Column Name>" />
              </Columns>
            </asp:GridView>
          </ItemTemplate>
        </asp:TemplateField>
         </Columns>
       </asp:GridView>

IN the child Gridview i have checkboxes that i will check all of them, look at the below event

OnRowDataBound="OnChidDataBound"

protected void OnChidDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.Header)
    {
      CheckBox chk = (CheckBox)e.Row.FindControl("chkHeader");
      chk.Attributes.Add("onclick", "javascript:SelectAll('" + chk.ClientID + "')");
    }
  }

here i am calling a js function...SelectAll

<script type="text/javascript">
    function SelectAll(ID)
    {
        var chk =  document.getElementById(ID);
         
        var chks =  document.form1.elements;
        for (i=0; i<chks.length; i++)
        {
           var type = chks[i].type;
           if (type=="checkbox")
           if (chk.checked)
           {
             chks[i].checked = true;
           }
           else chks[i].checked = false;
        }
    }
  </script>


Try this and let me know
Radhika roy replied to Anto Bilson on 16-Jul-11 03:41 AM

Follow these steps-


1) First you should find your GridView with the help of FindControl even if it's nested one.
 (You can use GridView1.Rows[0-based].FindControl)

to find the nested GridView or in the RowDataBound event by e.Row.FindControl.

2) When you find that and make a conversion from obj to GridView successfully, please use FindControl again to find the CheckBox ,in the specific row that you want .


Hope this will help you.