getting checked status of checkboxes in repeater

B

bmjnine

I finally found a solution to the problem of reading checkboxes from a
repeater, and since it took me forever to find, I just wanted to post
it here as well. The following is copied from DevShed, but I wanted to
add an important note: make sure you do not bind your repeater again
when you click your "submit" button, otherwise you'll lose the status
of all the checkboxes. Something like this:

if (IsPostBack) {
if (Request["mySubmitButton"] == null) {
urRepeater.DataBind();
}
}

Here's the original post (Thanks so much, Tom!):

----------------------------------

Its extremely simple.. ;-)
Just add a checkbox into ItemTemplate of your repeater. Let it be
"cbId"..so far, i donno how to set value for that repeater as it does
not support "value" property. A tricky idea is to store the value as
its "tooltip", Another option will be to store the value in a html
hidden field which will "runat server" using "<%#
DataBinder.Eval(Container.DataItem,"ur_Id")%>".

If you are planning to bind the repeater from a data table, that table
must contain a column named "ur_Id"..

To check whether a checkbox is checked on clicking a submit button;
just iterate through the repeater items and ind the checkbox control.
check out the following code:


for(int cnt = 0; cnt < urRepeater.Items.Count; cnt++)
{
// your checkbox ; type casting
CheckBox cbId = ((CheckBox)
urRepeater.Items[cnt].FindControl("cbId"));

// user id
int id = Convert.ToInt32(cbId.ToolTip);
// or the other option; value from hidden field

if (cbId.Checked)
{
// Your code
}
}

Best Regards
tom

----------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top