|
-
Oct 12th, 2007, 12:10 AM
#1
Thread Starter
Frenzied Member
[2005] How to determine which html checkboxes are checked?
I have an ASP page that generates a dynamic list at run time which contains an HTML (not ASP) Checkbox next to each item. My question is, once a PostBack occurs, how can I loop through all the Checkboxes with a given name such as:
Code:
<input id="clients" type="checkbox" value="1" />
<input id="clients" type="checkbox" value="2" />
<input id="clients" type="checkbox" value="3" />
In the code behind, I need to be able to loop through all Checkboxes with the ID of "clients" and determine the "value" and the "checked" property.
I don't want to use the checkboxlist control or datagrid, so please don't suggest that. I really need the above to work.
Any example would be appreciated.
-
Oct 12th, 2007, 06:46 AM
#2
Hyperactive Member
Re: [2005] How to determine which html checkboxes are checked?
Hi, I´m no expert, but have you tried adding the runat="server" attribute to each control?
-
Oct 12th, 2007, 10:57 AM
#3
Thread Starter
Frenzied Member
Re: [2005] How to determine which html checkboxes are checked?
 Originally Posted by Rauland
Hi, I´m no expert, but have you tried adding the runat="server" attribute to each control?
Hmmm... Didn't know you could do that with HTML controls. But, will this work if that code is generated by a response.write in the code behind on page load? I thought "runat", only works if it was statically created on the html page.
I'm not at my development computer now, but assuming this does work, I would still have to loop through the controls (unknown number at run time) an determine if it is checked. I don't know how to do this. Can someone post a quick example of iterating through each html checkbox in the codebehind in VB?
-
Oct 12th, 2007, 12:44 PM
#4
Hyperactive Member
Re: [2005] How to determine which html checkboxes are checked?
Hi,
I´m not sure whether this will help or not:
But have been doing a couple of tests.
Here´s the aspx page:
Supposing there are three radio buttons. Which belong to a group called, group1.
Also the result is shown in a literal control. I know you were asking for vb.net code! apologies for just posting in c# !
Code:
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Radio 1:<input id="Radio1" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
Radio 2:<input id="Radio2" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
Radio 3:<input id="Radio3" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
<asp:Literal runat="server" ID="testId"></asp:Literal>
</td>
</tr>
</table>
<input id="Submit1" type="submit" value="submit" />
</div>
</form>
</body>
And here´s the code behind code, so far in c#.
Code:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control ctr in this.form1.Controls)
{
if (ctr is HtmlInputRadioButton)
{
HtmlInputRadioButton rdBtn = ctr as HtmlInputRadioButton;
if (rdBtn.Name == "group1" && rdBtn.Checked==true)
{
testId.Text = string.Format("Radio button with <b>id:{0}</b> from radio button <b>group{1}</b> checked", rdBtn.ID, rdBtn.Name);
break;
}
}
}
}
}
-
Oct 12th, 2007, 02:41 PM
#5
Re: [2005] How to determine which html checkboxes are checked?
Looping through them is right and all, but you could try something... because those are controls that have been created and rendered onto the page, when your event occurs in which you want to detect which checkboxes have been selected, place a breakpoint anywhere and have a look inside Request.Form. I say this because a postback is nothing but an HTTP Post and you should therefore be able to see which checkboxes were selected.
Failing that, you could just use a checkboxlist and bind it to your 'list'.
-
Oct 12th, 2007, 03:46 PM
#6
Thread Starter
Frenzied Member
Re: [2005] How to determine which html checkboxes are checked?
 Originally Posted by Rauland
Hi,
I´m not sure whether this will help or not:
But have been doing a couple of tests.
Here´s the aspx page:
Supposing there are three radio buttons. Which belong to a group called, group1.
Also the result is shown in a literal control. I know you were asking for vb.net code! apologies for just posting in c# !
Code:
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Radio 1:<input id="Radio1" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
Radio 2:<input id="Radio2" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
Radio 3:<input id="Radio3" runat="server" name="group1" type="radio" />
</td>
</tr>
<tr>
<td>
<asp:Literal runat="server" ID="testId"></asp:Literal>
</td>
</tr>
</table>
<input id="Submit1" type="submit" value="submit" />
</div>
</form>
</body>
And here´s the code behind code, so far in c#.
Code:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control ctr in this.form1.Controls)
{
if (ctr is HtmlInputRadioButton)
{
HtmlInputRadioButton rdBtn = ctr as HtmlInputRadioButton;
if (rdBtn.Name == "group1" && rdBtn.Checked==true)
{
testId.Text = string.Format("Radio button with <b>id:{0}</b> from radio button <b>group{1}</b> checked", rdBtn.ID, rdBtn.Name);
break;
}
}
}
}
}
This might work. I'll give it a shot when I get home. Thanks!
-
Oct 12th, 2007, 03:48 PM
#7
Thread Starter
Frenzied Member
Re: [2005] How to determine which html checkboxes are checked?
 Originally Posted by mendhak
Looping through them is right and all, but you could try something... because those are controls that have been created and rendered onto the page, when your event occurs in which you want to detect which checkboxes have been selected, place a breakpoint anywhere and have a look inside Request.Form. I say this because a postback is nothing but an HTTP Post and you should therefore be able to see which checkboxes were selected.
Failing that, you could just use a checkboxlist and bind it to your 'list'.
But how flexible is a CheckboxList? Can it be embedded in a table like:
Code:
<table>
<tr>
<td>Checkbox1</td><td>Name</td>
<td>Checkbox2</td><td>Name</td>
<td>Checkbox3</td><td>Name</td>
<td>Checkbox4</td><td>Name</td>
</tr>
</table>
-
Oct 16th, 2007, 08:05 AM
#8
Re: [2005] How to determine which html checkboxes are checked?
-
Oct 18th, 2007, 05:05 PM
#9
Thread Starter
Frenzied Member
Re: [2005] How to determine which html checkboxes are checked?
Hmmm.. so how does it now what table to display in? I don't see that part in the example code.
-
Oct 19th, 2007, 01:35 PM
#10
Re: [2005] How to determine which html checkboxes are checked?
What table to display in? You place that control in whichever cell it should show up in, and bind it to your data. It then creates the 'rows' itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|