Repeater with Checkboxes, handling CheckedChanged event?

P

Per Salmi

Hi!

I have checkboxes in a repeater control that uses databinding. Each item
contains a checkbox and I would like to use autopostback and handle the
CheckedChanged event for the CheckBoxes. I have been looking for a sample
but it seems hard to find.

The repeaters ItemCommand event doesn't seem to fire... I understand it is
only for the button controls.

I have used a loop to find selected checkboxes on postback before but now I
want to use autopostback and act on immediate changes.

Anyone got a sample for this kind of eventhandling?

/Per
 
J

Jeffrey Tan[MSFT]

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

Per Salmi

Ok, then I was on the right track anyway. It was the ItemCreated stuff
adding the CheckedChanged event handler that I needed. The event handler
idea hit me just after posting to the group and it worked. It is strange
that as soon as one gives up searching for working code samples and post to
the newsgroup then the pieces falls in place and the solution is obvious...
Nice to see that I ended up with just about the same code as your example
shows.

I must admit that your part of the code that binds the Checked property of
the CheckBox is very nice compared to mine. Got to do some more reading on
the <%# .. %>-stuff.

/Per


""Jeffrey Tan[MSFT]"" said:
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.
 
J

Jeffrey Tan[MSFT]

Hi Per,

Thanks very much for your feedback.

I am glad my reply makes sense to you.

You may refer to "Data-Binding Expressions for Web Forms Pages" below for
more information about databinding expression:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbcondata-bindingexpressions.asp

If you have any further concern, please feel free to tell me, I will help
you.

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top