0

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 ?

0

1 Answer 1

2

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 += ", ";
        }
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Yes, thats right! Then do you know how could I identify which checkbox is checked?
You can use a Label or Literal outside of the repeater and set the Text property to the ID of the checked checkboxes.
It's because of AutoPostBack="True", you need to remove it as in the EDIT 2 section.
Yes it's possible, use a hidden field as described in EDIT 3 section.
Did you change your repeater code exactly as the code in EDIT 3 section?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.