Monday, May 11, 2009

.NET annoyance 5 - asp:CheckBox tag

HTML allows you to check a checkbox and submit any value.
Web developers get used to that type of thing.
.NET has an asp:CheckBox that only allows you to submit "on" or nothing.

I have a search page that has 10 checkboxes. In java I would just simply put those checkbox submitted values into a query (after sql escaping them). The checkboxes are type1-5 and skill1-5.

<input type="checkbox" name="type1" value="AD"/>Active Duty
<input type="checkbox" name="type2" value="CIV"/>Civilian
...

select name, department, type, skill from personTable where type in (@type1,@type2,@type3,@type4, @type5) and skill in (@skill1, @skill2, @skill3, @skill4, @skill5)

Push the request values into that sql query and Bing bang bong done. Not in .NET using asp:checkbox tags though. I would have to have code behind with the value of each of those checkboxes. I hate to put things that could change in two places.

<asp:CheckBox ID="type1" runat="server" Text="Active Duty" />
<asp:CheckBox ID="type2" runat="server" Text="Civilian" />
...

(In this example I am using a asp:SqlDataSource peopleDS) Then in the codebehind (in
peopleDS_Selecting) I would have to put code in to modify the sql query.
if (!type1.Checked) e.Command.Parameters["@type1"].Value = "AD";
if (!type2.Checked) e.Command.Parameters["@type2"].Value = "CIV";
...

Very annoying. Now I have the sql query split between .. Nevermind. I'll setup a better example later.

No comments:

Post a Comment