Monday, June 8, 2009

In some projects, I need to create ASP.NET controls and HTML controls on a web page and need to get the values of the controls with Request.Form[] method. Because this is the only way we can take the values of HTML controls when they are not declared with runat=server. [You take a look at my other post which explains how to take values of HTML controls in c#.] When I declare ASP.NET checkbox on a page, the value Request.Form["checkboxname"] always returns on or off. So, I never get the value of it. Because the ASP.NET checkbox don't have attribute for Value. So this is the same problem with <asp:CheckboxList> control. When I bind the data source for it, on the page it renders as two controls for each check box. One is Input with type checkbox and another is Label which holds the value as display text. So when I try to get the value I never get the check box value at all. i.e. if I declare checkboxlist for showing check boxes as ASP.NET, SharePoint, Java and want to know the value of user selection  by using Request.Form[], I always get the value on or off but not actual strings. So, how to get the value from the checkbox?

In these type of scenarios, I never use <asp:checkboxlist> on my web page. I always use multiple check boxes as a list to render on the page. This is because of getting the value in the Request.Form[""].

We need to declare a checkbox on the page and then use the below  line in c# code as shown below to set the value attribute manually.

cb.InputAttributes.Add("value", "checkboxvalue");

[If you need to build the checkboxes dynamically, then in for loop you can write the string which has the HTML check box declaration with input type="checkbox” and can access them in C# code.] So that it will render the value attribute for it. InputAttribute is the function we need to use to render it. So the final out put which render on the browser side looks like this.

<input type="checkbox" id="cb" name="cb" value="checkboxvalue" />

So, now when you try to get the checkbox value using

Request.Form["cb"] you will get the original value instead of the on or off.

And see my other post related to it, which explains you on "how to access the checkbox list values on client side".

Hope this will help to the guys, who are having the same requirements. Please give me your valuable feedback/comments. Do you think, is there any approach which solves the problem in easy way?

Categories: , ,


  1. hi praveen ,

    you can instead do a custom control that inherits from a checkbox list

  2. Yes, it is the second way of doing it. But any where or any one can finally set the value attribute only!!!. I preferred this way and you preferred that way. Thanks for the suggestion.