This is a migrated thread and some comments may be shown as answers.

How to fire server event when clicking checkbox

3 Answers 270 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Iron
Iron
Iron
Al asked on 01 Aug 2015, 09:21 AM

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?

 

 

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Maria Ilieva
Telerik team
answered on 04 Aug 2015, 02:47 PM
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
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 08 Aug 2015, 08:35 AM

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;
            }
        }
    }
}

0
Maria Ilieva
Telerik team
answered on 12 Aug 2015, 10:35 AM
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
Tags
ListView
Asked by
Al
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Maria Ilieva
Telerik team
Al
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or