Hi Per,
Thank you for posting in the community!
Based on my understanding, you use Repeater control in your web form
application. In the repeater control, you add checkbox column, which use
AutoPostBack attribute. You want to know how to add event handler for the
checkbox. Also, how to handle Item_Command event for your repeater control.
=========================================
Repeater control is similiar with DataGrid control. In internet, there are
a lot of datagrid control samples, which may give you hint.
Please refer to:
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx
and
http://msdn.microsoft.com/msdnmag/issues/02/03/cutting/
Also, for your specifically problem, I have writen a sample for you:
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox AutoPostBack=True ID="cb" Runat=server
Checked='<%#getchecked(Container.DataItem)%>'>
</asp:CheckBox>
<asp:Button ID="bt" Runat="server" Text="Button"></asp:Button>
<br>
</ItemTemplate>
</asp:Repeater>
protected bool getchecked(object dataitem)
{
DataRowView drv=(DataRowView)dataitem;
System.Int16 val=(System.Int16)drv["job_id"];
if(val>5)
{
return true;
}
else
{
return false;
}
}
protected System.Web.UI.WebControls.Repeater Repeater1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from jobs",
"server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);
Repeater1.DataSource=ds;
Repeater1.DataBind();
}
}
private void Repeater1_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
this.Response.Write("Repeater1_ItemCommand<br>");
}
private void Repeater1_ItemCreated(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
RepeaterItem ri=(RepeaterItem)e.Item;
if(ri.ItemType==ListItemType.Item||ri.ItemType==ListItemType.AlternatingItem
)
{
CheckBox cb=ri.FindControl("cb") as CheckBox;
cb.CheckedChanged +=new EventHandler(cb_CheckedChanged);
}
}
private void cb_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb=(CheckBox)sender;
if(cb.Checked)
{
this.Response.Write("Checked<br>");
}
else
{
this.Response.Write("Unchecked<br>");
}
}
In the sample code, I use SqlSever's default "jobs" table in "pubs"
database. For checkbox, I determine the "jobs_id" field in the table, if
the field value is larger than 5, then set the checkbox to checked.
Note: in the Page_Load event, you should determine if the page is postback,
then not rebind the repeater control when postback. Or your checkbox
checkedchange event will be overlayed.
=======================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.