Today Luvy and I looked at updating the logic for an ASP.NET forms page that we are working on. The page contains a multi-row update-able GridView, and we were asked to make sure that if one CheckBox control was unchecked, that another CheckBox control on the same grid line would also uncheck itself.
In our case, we are performing a number of control adjustments in the GridView’s Render event, and I like to keep GridView logic in one place for readability reasons. So we went with the option of instantiating & updating the control in the GridView’s Render event rather than the CheckBox control’s Render event.
To summarize the code, it went something a little like this:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim cbxIncludeInRevPlanLoadDtl As CheckBox = CType(e.Row.FindControl("cbxIncludeInRevPlanLoadDtl"), CheckBox) cbxIncludeInRevPlanLoadDtl.Attributes.Add("onClick", "autoUnCheckExtend(this)")
Specifically, it receives a reference of the CheckBox control that fired the onClick event. The function then parses out the important location part of the control ID and replaces it with the name of the control that needs to be changed.
The way that ASP.NET renders the HTML for GridView controls is to prefix location information for the control into the ID information of the control. So, for example, in a GridView called GridView1 a CheckBox control would be rendered as:
<input id="GridView1_ctl02_cbxExtendInRevPlanLoadDtl" name="GridView1$ctl02$cbxExtendInRevPlanLoadDtl" CHECKED="checked" type="checkbox">
Then on the subsequent grid line the CheckBox control would be rendered as:
The function then takes the ID of the control and parses out the location information that it then uses to identify the other CheckBox control on the same line to check.
Using these methods to manage ASP.NET objects via client side script can be quite useful in managing the behavior of a Web page without making use of heavyweight operations that require a call to the server.