Accessing Controls Inside Gridview Using C#/Jquery

We know that accessing controls from C# code is a much preferred way over accessing from the Client side. Sometimes we are required to access them from the client side too and this is something, which most of us find a little challenging.

First we will start with C#

Suppose, we have a Gridview in our page, as given below-

  1. <asp:GridView ID="grdviewnew" runat="server" CellPadding="10" OnRowDataBound="grd_RowDataBound"  
  2.     AutoGenerateColumns="false" OnRowCommand="grd_Rowcommand">  
  3.     <Columns>  
  4.         <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Name">  
  5.             <ItemTemplate>  
  6.                 <asp:Label ID="lbltext" runat="server" Text='<%#Eval("name") %>'></asp:Label>  
  7.             </ItemTemplate>  
  8.         </asp:TemplateField>  
  9.         <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Mobile">  
  10.             <ItemTemplate>  
  11.                 <div id="divdata" runat="server">  
  12.                     <%#Eval("mobile") %>  
  13.                 </div>  
  14.             </ItemTemplate>  
  15.         </asp:TemplateField>  
  16.         <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Action">  
  17.             <ItemTemplate>  
  18.                 <asp:Button ID="btngetval" Text="Update Data" runat="server" CommandName="UpdateData"  
  19.                     CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />  
  20.             </ItemTemplate>  
  21.         </asp:TemplateField>  
  22.     </Columns>  
  23. </asp:GridView>   

As you can see, there are three columns in this Gridview. First column has an ASP Label, second has an HTML Div tag and third column contains a button.

Now, we will bind the Gridview with some data. 

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         DataTable dt = new DataTable();  
  6.         dt.Columns.Add("name");  
  7.         dt.Columns.Add("mobile");  
  8.         DataRow dru = dt.NewRow();  
  9.         dru["name"] = "Raj";  
  10.         dru["mobile"] = "9999999999";  
  11.         dt.Rows.Add(dru);  
  12.         dru = dt.NewRow();  
  13.         dru["name"] = "Sam";  
  14.         dru["mobile"] = "8888888888";  
  15.         dt.Rows.Add(dru);  
  16.         dru = dt.NewRow();  
  17.         dru["name"] = "Ajay";  
  18.         dru["mobile"] = "7777777777";  
  19.         dt.Rows.Add(dru);  
  20.         dru = dt.NewRow();  
  21.         dru["name"] = "Nitin";  
  22.         dru["mobile"] = "6666666666";  
  23.         dt.Rows.Add(dru);  
  24.         dt.TableName = "table1";  
  25.         grdviewnew.DataSource = dt;  
  26.         grdviewnew.DataBind();  
  27.     }  
  28. }   

Now, our Gridview is bound with four rows, as given below-


Now, when we click the button in third column, we need to update the Label in First column as well as Div in second column.

We will achieve this, using “OnRowCommand” event of Gridview.

The code is given below-

  1. protected void grd_Rowcommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         if (e.CommandName == "UpdateData")  
  6.         {  
  7.             //Getting the index of Row to update  
  8.             int index = Convert.ToInt32(e.CommandArgument);  
  9.   
  10.             //Here we access the Label   
  11.             Label lbl = (Label)grdviewnew.Rows[index].FindControl("lbltext");  
  12.             lbl.Text = "Updated Label";  
  13.   
  14.             //Here we access the Div  
  15.             HtmlGenericControl mydiv = (HtmlGenericControl)grdviewnew.Rows[index].FindControl("divdata");  
  16.             mydiv.InnerHtml = "Updated Div";  
  17.         }  
  18.     }  
  19.     catch (Exception ex)  
  20.     {  
  21.     }  
  22. }   

You can clearly see and understand from the code, given above, how to access and update Label as well as Div in Gridview. In a similar way, we can access each and every control inside Gridview.

Now on clicking the button, the corresponding row will be updated.


Now we will look how to do the same from client side - using Jquery!

For this, our Gridview will be slightly different, as given below-

  1. <asp:GridView ID="grdviewnew" runat="server" CellPadding="10" OnRowDataBound="grd_RowDataBound"  
  2.             AutoGenerateColumns="false">  
  3.             <Columns>  
  4.                 <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Name">  
  5.                     <ItemTemplate>  
  6.                         <asp:Label ID="lbltext" runat="server" Text='<%#Eval("name") %>'></asp:Label>  
  7.                     </ItemTemplate>  
  8.                 </asp:TemplateField>  
  9.                 <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Mobile">  
  10.                     <ItemTemplate>  
  11.                         <div id="divdata" runat="server">  
  12.                             <%#Eval("mobile") %>  
  13.                         </div>  
  14.                     </ItemTemplate>  
  15.                 </asp:TemplateField>  
  16.                 <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Action">  
  17.                     <ItemTemplate>  
  18.                         <asp:Button ID="btngetval" Text="Update Data" runat="server" OnClientClick="return updatedata(this.id)" />  
  19.                     </ItemTemplate>  
  20.                 </asp:TemplateField>  
  21.             </Columns>  
  22.         </asp:GridView>   

As you can see, we removed “OnRowCommand” from Gridview as well as removed “CommandName” & “CommandArgument” from the button.

We have also called a JavaScript function "updatedata" in button's "OnClientClick". We now bind Gridview with the data, which will be using similar step, as provided above. Now, our Gridview has four rows and on click of the button, we need to update the Label and Div.

Thus, our JavaScript function "updatedata" is given below-

  1. <script type="text/javascript">  
  2.         function updatedata(id) {  
  3.             //Splitting the Button ID  
  4.             id = id.split('_');  
  5.               
  6.             //Assigning the unique number in the ID to a variable  
  7.             val = id[id.length - 1];  
  8.   
  9.             //Updating the Label and Div  
  10.             $('#grdviewnew_lbltext_' + val).html('Updated Label');  
  11.             $('#grdviewnew_divdata_' + val).html('Updated Div');  
  12.             return false;  
  13.         }  
  14.     </script>   

We are passing the "ID" of the button as an argument to this function. Now, the most important thing to note is how we are getting the ID of controls inside the Gridview. We know that even if we give an ID to our controls in Gridview, these ID's will change for the client side on run time. There is a trick to identify those ID's. They all follow a pattern. For each row, the ID of the controls will change only in the last auto increment number.

The screenshot of the Browser's inspect element of our gridview is given below-

As you can see, the ID of the Label in the first row and second row differs only by an auto increment number, i.e starting from 0, it adds +1 to each row.

Also, all the ID will be having a general format - Gridviewid_LabelID_Autoincrementnumber.

We may need to find out ID of the particular control in one row by checking the Browser's inspect element and based on it, we can set it in our function.

Please note that above ID pattern may change with respect to the structure of your page, as you like, if you have a master page or an update panel, the above pattern may change. The last auto-increment number will be there and this is the key!

Now, coming to our JavaScript function, we have already discussed, we are passing ID of the button as an argument. This will help us to find the auto increment number for that particular row by splitting it out from the button ID. Now, we will get ID of our Label and Div. With this, it updates.


That's it! 

Conclusion

In this article, we learned, how to access and update the controls inside Gridview from the Server Side as well as the Client Side!

If you are an expert in jQuery, you can simply make the use of parent, children, siblings, etc. in jQuery for the same purpose. The method, provided above is relatively simple way!

Hope, this article will be helpful.


Similar Articles