Checkbox in Repeaters

B

Beginner

I have the following aspx and code behind to put a checkbox in repeater.
There are several problems:

- CheckBox2_CheckedChanged works well. But I want to know who is being
checked, i.e. how can I pass userid back as an argument? (I can get checkbox
client ID or checkbox text, but they do not serve the purpose well)

- repRoles_ItemCommand seems never fired, any hints on that?

Thanks.


==========================================================
<asp:repeater id="repRoles" runat="server"
OnItemCreated="repRoles_ItemCreated"
OnItemCommand="repRoles_ItemCommand">

<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "userid")%></td>
<td><asp:CheckBox id="CheckBox2"
AutoPostBack=True runat="server"
Checked='<%#(DataBinder.Eval(Container.DataItem,
"manager").ToString()=="1")
?true:false%>'></asp:CheckBox>
</td>
</tr>
</ItemTemplate>

===========================================================
protected void repRoles_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e) {
this.Response.Write("repRoles_ItemCommand<br>");
Response.Write("Name:");
Response.Write(e.CommandName);
}

protected void repRoles_ItemCreated(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e) {

RepeaterItem ri=(RepeaterItem)e.Item;


if(ri.ItemType==ListItemType.Item||ri.ItemType==ListItemType.AlternatingItem
) {
CheckBox CheckBox2=ri.FindControl("CheckBox2") as CheckBox;
CheckBox2.CheckedChanged +=new EventHandler(CheckBox2_CheckedChanged);
}
}

private void CheckBox2_CheckedChanged(object sender, EventArgs e) {

CheckBox CheckBox2=(CheckBox)sender;
Response.Write(CheckBox2.ClientID);
if(CheckBox2.Checked) {
this.Response.Write("Checked<br>");
} else {
this.Response.Write("Unchecked<br>");
}
}
 
T

Teemu Keiski

ItemCommand fires only if there is a control causing postback (in the item)
that has Command event e.g which initiates event bubbling. By default such
controls are Button, ImageButton and LinkButton.

To get to the ID:

1. Now, you bind the userid to the first cell in table. Put there a Label or
similar and bind the ID to it.
2. In CheckedChanged event handler you can get reference to the current
RepeaterItem via the sender (CheckBox itself). SOmething like
CheckBox2.Parent should return it (with proper casting of course). From this
referenced RepeaterItem you can locate the Label (where userid was being
bound) by using RepeaterItem's FindControl method. This will search from the
same "row"/Item where the CheckBox itself was located and should serve your
purpose.
 
B

Beginner

Thanks. I end up using CheckBox2.Text and set font size to 1px and color to
white ^_^
 

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

Top