Referencing ASP.NET CheckBox Controls using strings and casting

Category: Code ModeASP.NET – I recently ported one of my older web ASP Application to a web ASP.NET Application which presented a few challenges along the way. One of these challenges was a need to manage a series of 30 check boxes within a form. This part of the application is used to manage user permissions, restricting what areas of the application individual users had access to.

The user permissions are stored in a database as a pipe (“|”) delimited string. when the page containing the form loads, the permission string is pulled from the database, and split into an array.

When loading a page, all the checkboxes that indicate a users permission need to be checked.

In good ol’e fashioned ASP, all the checkboxes in a form would be given the same “name” attribute, and a “value” attribute containing the permission string. When the page containing the form was loaded, a piece of client-side javascript would run and iterate through all the checkboxes. For each checkbox in the form, the script would then iterate through each permission array item and if the item matched the checkbox value attribute, the checkbox state would be set to “checked”.

ASP Form

<form name="permissions" method="post" action="process_page.asp">
 <table width="461" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td>
   <input type="checkbox" name="setPermission" value="user_add">Add</ br>
   <input type="checkbox" name="setPermission" value="user_manage" />Manage</ br>
   <input type="checkbox" name="setPermission" value="user_delete">Delete</ br>
   <input type="checkbox" name="setPermission" value="user_edit"> Edit</br>
   <input type="checkbox" name="setPermission" value="user_setVisibility" /> set visibility</br>
  </td>
  </tr>
 </table>
</form>

When the ASP page is submitted after altering user permissions (checking or unchecking boxes), server-side code gets the values of all the checked checkboxes using Request.Form(“Checkbox_name”) .value, converts it to a pipe (“|”) delimited string, and updates the database with the new delimited string.

When porting this to ASP.NET, we run into a problem when replacing the standard form checkboxes to ASP.NET CheckBox Controls. .Net CheckBox controls cannot have the same “name” attribute so we cannot access them as a group like we did in ASP. A workaround for this is to give each CheckBox Control an ID equal to the permission value.

ASP.NET Form

<form action="" method="post" name="CMSForm" id="CMSForm" runat="server">
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td class="p_text1" width="67">
    <asp:CheckBox ID="user_add" Text="Add" runat="server" cbValue="add_user"/>
    <asp:CheckBox ID="user_manage" Text='View/Manage' runat="server" cbValue="view_user_manager"/>
    <asp:CheckBox ID="user_delete" Text='Delete' runat="server" cbValue="del_user"/>
    <asp:CheckBox ID="user_edit" Text='Edit' runat="server" cbValue="edit_user"/>
    <asp:CheckBox ID="user_setVisibility" Text='Set Visibility' runat="server" cbValue="hide_show_user"/>
   </td>
  </tr>
 </table>
</form>

Now for the FUN.NET

Now instead of using a full block of client-side javascript and nested loops as we did in ASP, we can access the ASP.NET CheckBox Controls directly using a few lines of server-side code and Casting.

The key to this is the change from using a “value” attribute to contain the permission value to using the Control’s ID to contain the permission value. Because the permission values are stored and pulled from a database, and split into an array, each array item can also be used to reference the CheckBox Controls directly without having to loop through each control.

To set the checked state of the CheckBox controls, we loop through the permissions array and Cast the string as a CheckBox Control allowing us to access that specific control.

I am using a MySql database here:
First we need to get or data from the database. “access” is the DB table column that stores my permission string for each user.

MySqlConnection MyConString = new MySqlConnection(...your datababase connection string here...);
MySqlCommand myCommand = new MySqlCommand("SELECT  access FROM <your dataTable> WHERE emp_ID = <unique ID of user you want to manage> ", MyConString);
MyConString.Open();
MySqlDataReader dr = myCommand.ExecuteReader();

Next we get the string value of “access” and split it into an array.

try{
 dr.Read();
}
catch(Exception ex){
 however you want ot handle the exception here
}
char[] splitter  = {'|'};
string[] arrPermissions = dr["access"].ToString().Split(splitter);

setCheckboxes(arrPermissions);

setCheckboxes Method (this is where the magic happens)
This method takes the permissions array as a argument, iterates through the array, casting the string value of each item as a CheckBox, and sets the Checked state of all matching CheckBox Controls.

private void setCheckboxes(string[] arrPermissions)
{
foreach(string strPermission in arrPermissions){
((CheckBox)this.CMSForm.FindControl(strPermission)).Checked = true;
}
}

The same technique can be applied to other .NET controls as well to change their attributes, styles, or whatever.
I could have just as easily changed the text of the controls using something like: ((CheckBox)this.CMSForm.FindControl(strPermission)).Text= “I am a CheckBox Control”;

One Reply to “Referencing ASP.NET CheckBox Controls using strings and casting”

  1. I have simple html checkboxes in my asp.net page and a submit button. But when i click submit button i am unable to get checkboxes in codebehind and there status? I tried using request.form collection but there are no checkboxes in it?

    how can i solve the issue?

Comments are closed.