Reputation: 245
In my project, there is a Repeater
. And in the repeater is a CheckBox
. When I bind data to the Repeater
, how can I control (set checked / unchecked) the CheckBox
s that are produced by the Repeater
?
This is what I've tried:
<asp:Repeater ID="Security1" runat="server">
<ItemTemplate>
<tr>
<td> <asp:CheckBox ID="CheckBox1" runat="server"> </td>
<td><%#DataBinder.Eval(Container.DataItem,"Featurename") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Upvotes: 0
Views: 4300
Reputation: 312
You can try something like this ;
< asp:repeater id="repeater" runat="server">
< li>< asp:checkbox id="chkbx" runat="server />
< %#Eval("attribute_name_here")%>< /li>
< /asp:repeater>
Code behind , adding properties to checkboxes ;
protected void repeater_ItemDataBound(...)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
((CheckBox)e.Item.FindControl("chkbx")).Attributes
.Add("project_id",
((DataRowView)e.Item.DataItem)["project_id"].ToString());
}
}
Then when you want to check them ; you can also check attribute in if of course
foreach (RepeaterItem item in repeater.Items)
{
CheckBox chkbx = item.FindControl("chkbx") as CheckBox;
if (chkbx.Checked)
{
Response.Write("Checked Project: " +
chkbx.Attributes["project_id"].ToString() + "< br />");
}
}
Upvotes: 0
Reputation: 2638
I'm assuming that 'Featurename' is a bit/bool value, so if you want to control the 'checked' state you would bind it to the 'Checked' property of the checkbox control:
Something like this...
<asp:checkbox id="check1" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"Featurename") %>'/>
Upvotes: 1