How to fire server event when clicking checkbox

4 posts, 1 answers
  1. Al
    Al avatar
    203 posts
    Member since:
    Oct 2006

    Posted 01 Aug 2015 Link to this post

    Hi,

    I have listview that works well with the definition below:

    <telerik:RadListView ID="lvwUserGroups" runat="server" ItemPlaceholderID="groupsPlaceholder" OnNeedDataSource="lvwUserGroups_NeedDataSource"
        DataKeyNames="GroupID" >
        <LayoutTemplate>
            <div class="RadListView RadListView_<%# Master.SkinManager.Skin %>" >
                <table class="mainTable">
                    <thead>
                        <tr class="rlvHeader">
                            <th class="thcenter">Enabled</th>
                            <th>Group</th>
                        </tr>
                    </thead>
                    <tbody>
                        <asp:PlaceHolder id="groupsPlaceholder" runat="server">
                        </asp:PlaceHolder>
                    </tbody>
                </table>
            </div>
        </LayoutTemplate>
    </telerik:RadListView>

     

    protected class userGRPTemplate : ITemplate
    {
        private ADScrubWeb.HTMLHelper helper = new ADScrubWeb.HTMLHelper();
     
        public userGRPTemplate()
        {
        }
     
        public void InstantiateIn(Control container)
        {
            TableRow tr = new TableRow();
            container.Controls.Add(tr);
     
            helper.AddControlToParent(container, helper.AddBoundHiddenField("GroupID"));
     
            helper.AddControlToParent(tr, helper.AddBoundCheckBox("Enabled"), true, HorizontalAlign.Center);
            helper.AddControlToParent(tr, helper.AddBoundTextBox("GroupName", BorderStyle.None, true), true, HorizontalAlign.Left);               
        }
    }

     

    public CheckBox AddBoundCheckBox(string controlID)
    {
        CheckBox tmp = new CheckBox();
        tmp.ID = controlID;
        tmp.DataBinding += delegate(object sender, EventArgs args)
        {
            CheckBox control = ((CheckBox)sender);
            RadListViewDataItem listViewDataItem = ((RadListViewDataItem)control.NamingContainer);
            control.Checked = lsuGeneral.IsTrue(DataBinder.Eval(listViewDataItem.DataItem, controlID).ToString());
        };
        return tmp;
    }

     

    I would like to react to the user clicking the bound checkbox so I can do auto-updates without the user clicking an update button. I tried wiring up the checkbox CheckedChange event in the ITemplate but that didn't fire, anyway I would still need to know which row's checkbox was clicked.

    Any ideas?

     

     

     

  2. Answer
    Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 04 Aug 2015 Link to this post

    Hello,

    Please try setting the AutoPostback property of the CheckBox control to "true" and see how it goes.


    Regards,
    Maria Ilieva
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Al
    Al avatar
    203 posts
    Member since:
    Oct 2006

    Posted 08 Aug 2015 Link to this post

    Thanks Maria, I missed that. For anyone else interested, I used something like this in my listview template:

     

        public void InstantiateIn(Control container)
        {
            CheckBox chkIsKey = helper.AddBoundCheckBox("IsKey");
            chkIsKey.AutoPostBack = true;
            chkIsKey.CheckedChanged += chkIsKey_CheckedChanged;
     
            helper.AddControlToParent(chkIsKey);
     
        }
     
        void chkIsKey_CheckedChanged(object sender, EventArgs e)
        {               
            CheckBox chk = ((CheckBox)sender);
            if (chk.Checked)
            {
                RadListViewDataItem listViewDataItem = ((RadListViewDataItem)chk.NamingContainer);
                 
                //Do something with other controls on this row
                CheckBox someOtherCheckbox = ((CheckBox)listViewDataItem.FindControl("someOtherCheckbox"));
                someOtherCheckbox.Checked = true;
     
     
                //Do something with other controls in this list
                RadListView listView = listViewDataItem.OwnerListView;
                foreach (RadListViewDataItem dataItem in listView.Items)
                {
                    CheckBox xx = ((CheckBox)dataItem.FindControl("xx"));
                    xx.Checked = false;
                }
            }
        }
    }

  5. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 12 Aug 2015 Link to this post

    Hi Al,

    Thank you for sharing your solution. I'm sure it will help other users to implement such functionality.

    Regards,
    Maria Ilieva
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top