In our day to day ASP.NET programming we would have used GridView for one or the other purpose. There would have been requirement to access the controls inside a GridView from javascript. Whenever I had to use the ASP.NET GridView control, nearly 95% of the time I had to access the controls inside the GridView using javascript for one or the other purpose. With this blog I wanted to show how easily you can retrieve controls rendered inside a GridView from javascript. I would like to highlight this by taking an age old design where you will have a checkbox in the header and another set of checkboxes in the column below. Checking the checkbox in the header would select all the checkboxes in the column and un-checking a single checkbox in the column would un-check the header checkbox. This strategy can be used to access any type of controls not only inside the GridView but other data rendering controls as well. Now lets see the code for the same.
<asp:GridView ID="gvChk" runat="server" AutoGenerateColumns="False" |
The code is very simple with a GridView control having two columns. First column has a label control and second one has a checkbox control. The second column has a checkbox in the header as well. When checkbox in the header is clicked all the checkboxes in the column are selected using javascript. The javascript code to select all the checkboxes in the column are pasted below.
function selectAllCheckboxInGrid(gridID, chk) |
The above pasted javascript is straight forward. The javascript function takes two arguments, one is the GridView’ client id and the second is the checkbox in the GridView control’ header. The function starts by getting the grid control using the “getElementById” javascript method of the document object. After getting the GridView control, which is converted to a HTML table once rendered to the browser, we are looping through the rows collection starting from the first row. The zeroth row is the header so we don’t want the loop to start from the header. Starting from the 1st row we loop through the whole rows and get the controls in the second cell of the GridView using the “getElementsByTagName” method of the document object. Then we loop through the control array and check the type of the control and if it is a checkbox we set the checked property based on the checkbox control of the header cell passed as an argument to the javascript function. If you have only one control in the cell then you can directly use “cells[1].children[0].checked = chk.checked” so that you can avoid the loop. The above modified code is pasted below.
function selectAllCheckboxInGrid(gridID, chk) |
Next we have to have a function which will check the header checkbox if all the checkbox in the column are checked and if out of all the checked checkboxes anyone is unchecked then we need to uncheck the header checkbox. The simple function is fired when any of the checkboxes in the column are clicked. The javascript function is pasted below.
function checkUncheckHeaderCheckBox(gridID) |
The above function is pretty straight forward. The “GridView” control id is passed as argument. The function gets the “GridView” control using the “getElementById” function of the document object and loops through the rows starting from the first row leaving the zeroth row. Inside the loop the function retrieves the controls inside the second cell of each row and checks whether any one of the checkbox is unchecked. If so it assigns the “checked” variable as false and breaks the loop. Finally it sets the checked property of the checkbox in the header (zeroth row) to the value of “checked” variable. This function will take care of checking or unchecking the checkbox if all the checkbox in the column are checked or if any one checkbox in the checkbox column is unchecked respectively.
To fire the javascript function on the click of the checkboxes we need to add the javascript function to the “onclick” javascript event. To do this add the following in the “RowDataBound” event of the GridView control.
protected void gvChk_RowDataBound(object sender, GridViewRowEventArgs e) |
With the above code we are ready to go.
The above technique using javascript can be used to retrieve any type of controls (textbox, dropdown, radio button, button, links, images, labels etc) from within the GridView control or any other ASP.NET data rendering controls. You need to make some small javascript changes according to your data bound controls layout. So tweak the code wherever necessary and start retrieving controls.
Try to know more…
Sandeep
GridView .NET control
ReplyDeleteYou can retrieve controls from an ASP.NET HostMyCode GridView using JavaScript by accessing the specific row and control with something like var controlValue = document.get _RowIndex_ColumnName.
ReplyDeleteRetrieving controls from an ASP.NET GridView using JavaScript is a useful technique when you want to dynamically interact with individual cells or data in the GridView. By using JavaScript to access controls like textboxes, labels, or buttons within the GridView, you can enhance the interactivity and functionality of your web application. For streamlining your workflow and enhancing productivity, platforms like naps2 can be very helpful. This topic is essential for anyone working with ASP.NET controls in client-side scripting!
ReplyDelete