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
----------------------------------
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
----------------------------------