Checkbox OnClick/ItemCommand in Repeater or DataList
Asked Answered
E

3

6

I need to do some server side logic on a row in my repeater when a CheckBox is clicked inside the repeater control.

Anyone know how to go about this?

The way I see it you cant fire item command and if you use the CheckBoxes OnClick you cant get the repeater row.

Evenings answered 25/8, 2010 at 14:35 Comment(1)
Here is an answer: frankwisniewski.net/2013/01/15/…Aweinspiring
S
9

Here is a quick mock-up of how I have done similar in the past.

    <asp:Repeater id="repeater1" runat="server" OnItemDataBound="repeater1_OnItemDataBound" >
        <ItemTemplate>
            <asp:CheckBox ID="chk" runat="server" OnCheckedChanged="Check_Changed" AutoPostBack="true" />
        </ItemTemplate>
    </asp:Repeater>

codebehind:

    public class Model {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public partial class Checkboxes : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if(!IsPostBack ) {
                repeater1.DataSource = new List<Model> { 
                               new Model { Id = 1, Name = "a" }, 
                               new Model { Id = 2, Name = "b" }, 
                               new Model { Id = 3, Name = "c" } };
                repeater1.DataBind();
            }
        }

        protected void repeater1_OnItemDataBound(Object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                var item = e.Item.DataItem as Model;
                if (item != null) {
                    var chk = e.Item.FindControl("chk") as CheckBox;
                    if (chk != null) {
                        chk.Text = item.Name;
                        chk.InputAttributes.Add("value", item.Id.ToString());
                    }
                }
            }
        }

        protected void Check_Changed(Object sender, EventArgs e) {
            var id = ((CheckBox) sender).InputAttributes["value"];
            //you now have access to the item id and can manipulate at will.
        }
    }
Satisfy answered 25/8, 2010 at 14:41 Comment(3)
Thanks this should work. Wish there was a less round about way... +1Evenings
It depends on what you are trying to do with the data. If it is a simple database flag update for a single row, an AJAX callback hooked up through JQuery would be a better and cleaner approach. In fact, it is probably a cleaner approach, no matter what you are doing with the data. It would allow users to change multiple checkboxes without postbacks.Satisfy
That's what I ended up doing. Thanks for the mockup though. It clearly illustrated how overly complicated it was and steered me in the JQuery direction. =DEvenings
O
2

Try this codebehind:

 protected void Checked_Changed(object sender, EventArgs e)
        {
            var item = ((CheckBox)sender).Parent as RepeaterItem;
// now you have the repeater row. You can travers further up the controls if you use Parent.Parent...

        }
Outguard answered 25/4, 2013 at 13:11 Comment(0)
V
-1

You could use the OnClick event to loop through each item in the repeater, and check the value of each checkbox, (IsChecked == true).

Just make sure that you are not calling a "DataBind()" on the repeater, otherwise that might cause issues.

Vladi answered 25/8, 2010 at 14:43 Comment(2)
So I will have to keep a record of the current state if I want OnChange functionality? Is there not another way?Evenings
The current state is recorded in the properties of each checkbox, so you shouldn't have to do anything extra besides just loop through each checkbox.Vladi

© 2022 - 2025 — McMap. All rights reserved.