Reputation: 1295
I have checkboxes created in a repeater:
<asp:Repeater ID="companyRepeater" runat="server">
<ItemTemplate>
<tr class ="DGItemStyle" id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
.....
<td><asp:CheckBox id="foo" runat="server" AutoPostBack="True" name="foo" /></td>
</tr>
I want to find out which checkbox is currently selected by user, so in the codebehind I do:
CheckBox chkbox_All = FindControl("foo") as CheckBox;
if (chkbox_All != null)
{
if (!chkbox_All.Checked)
{
Response.Write("No checked");
}
else
{
var IDs = chkbox_All.ClientID;
Response.Write("ID here...");
}
}
But what I found is that the chkbox_All
is always null no matter how many checkboxes are selected.
Why is it so and how could I find which checkbox is checked in this case ?
Upvotes: 0
Views: 7112
Reputation: 14614
You need to enumerate through companyRepeater.Items
using a foreach
block as below
foreach (RepeaterItem ri in companyRepeater.Items)
{
CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
if (chkbox_All != null)
{
if (!chkbox_All.Checked)
{
Response.Write("No checked");
}
else
{
var IDs = chkbox_All.ClientID;
Response.Write("ID here...");
}
}
}
EDIT
If you want to display the ID of the checked checkboxes, add a Literal somewhere outside the repeater
<asp:Literal ID="ltChecked" runat="server" />
and add the id to ltChecked.Text
if the checkbox is checked
foreach (RepeaterItem ri in companyRepeater.Items)
{
CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
if (chkbox_All != null)
{
if (chkbox_All.Checked)
{
ltChecked.Text += chkbox_All.ClientID;
ltChecked.Text += ", ";
}
}
}
EDIT 2
To prevent the page reload when checking the checkbox, remove AutoPostBack="True"
from the foo
checkbox
<asp:Repeater ID="companyRepeater" runat="server">
<ItemTemplate>
<tr class = "DGItemStyle" id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
.....
<td><asp:CheckBox id="foo" runat="server" name="foo" /></td>
</tr>
EDIT 3
To get the compnName1
value from the same row as the checked checkbox, add a hidden field (hdCompName
) that holds the compnName1
value in the repeater
<asp:Repeater ID="companyRepeater" runat="server">
<ItemTemplate>
<tr class = "DGItemStyle" id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
.....
<td><asp:CheckBox id="foo" runat="server" name="foo" />
<asp:HiddenField id="hdCompName" runat="server" Value='<%# Eval("compnName1") %>' />
</td>
</tr>
then get the value of hdCompName
in the code behind as below
foreach (RepeaterItem ri in companyRepeater.Items)
{
CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
if (chkbox_All != null)
{
if (chkbox_All.Checked)
{
HiddenField hdCompName = ri.FindControl("hdCompName") as HiddenField;
ltChecked.Text += hdCompName.Value;
ltChecked.Text += ", ";
}
}
}
Upvotes: 2