开发者

Using checkbox in template column - denying multiple selections

开发者 https://www.devze.com 2023-02-16 09:31 出处:网络
Morning all. I have the followingh scenario where I have a radgrid and inside it, I have a template column containing a check box:

Morning all.

I have the followingh scenario where I have a radgrid and inside it, I have a template column containing a check box:

    <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Display Information" >
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:CheckBox ID="CheckBox1" runat=开发者_运维百科"server" AutoPostBack="true" OnCheckedChanged="CheckedChanged" />
</asp:Panel>
</ItemTemplate>
</telerik:GridTemplateColumn>

Upon being checked, the following event is fired, which in it's simplistic fashion, changes the style of the data items:

protected void CheckedChanged(object sender, EventArgs e)
        {
            {
                var chkBox = (sender as CheckBox);
                var myPanel = chkBox.Parent as Panel;
                var dataItem = myPanel.NamingContainer as GridDataItem;
                var cell = dataItem["Id"].Text;

                if (chkBox.Checked)
                {
                    dataItem["Id"].Style["color"] = "orange"; 
                    dataItem["Desc"].Style["color"] = "orange";                   
                }
                else
                {
                    dataItem["Id"].Style["color"] = "black"; 
                    dataItem["Desc"].Style["color"] = "black";
                }
            }
        }

This works as expected and does the job.

However, I only really want the user to be able to select one checkbox at a time.

Therefore, how to I go about ensuring that the any previous 'checks' are removed or stopping multiple checking altogether?

Any help or suggestions greatly appreciated.


First why not use a radion button instead and then give all the radio buttons the same NAME tag. That will "group" them together and only one can be selected at once.

Edit: Your other option if must have check boxes is to just clear them all out in the event in either code behind or in a JavaScript.

Cheers, Stefan

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号