Need help implementing a checkbox

2 posts, 0 answers
  1. toddhd
    toddhd avatar
    21 posts
    Member since:
    Aug 2006

    Posted 07 Dec 2010 Link to this post

    I am displaying a list of objects in a grid. One property of the object is a boolean value that I'd like to be able to allow the user to change simply checking/unchecking a checkbox. I want this to be a one-click solution - that is to say, I don't want them to have to select the row, go into edit mode, check the box, choose to save the records, etc. If the box gets checked, I want to update the record that it is bound to.

    Simple enough I think, but going about this seems so problematic.

    At first, I tried using a GridCheckBoxColumn. This correctly displays the value of the property, but is disabled. I learned that is because this will only enable if the row is in Edit mode. I added some code to the Pre_Render() event which causes every single row to be set to edit mode initially, but this looks pretty ugly - all the rows have dark lines on them, and I have to go back and explicitly set every other control in the row to ReadOnly so that they aren't editable.

    My second attempt involved using a GridTemplateColumn, and an asp:CheckBox which is bound (Bind(MyProperty)) to the property. This also displays the value fine, but I'm not sure how to handle it past that. Checking the box doesn't seem to update the bound data? I figured maybe I could set AutoPostBack=True and call the OnChecked() function to set the value manually, but how do I get a reference to the row/item being checked?

    Any help would be appreciated.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Dec 2010 Link to this post

    Hello Toddhd,

    The following code snippet shows how to enable GridCheckBoxColumn in normal mode.

    ASPX:
    <telerik:GridCheckBoxColumn DataField="isapproved" UniqueName="GridCheckBoxColumn">
    </telerik:GridCheckBoxColumn>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                CheckBox chkbox = (CheckBox)item["GridCheckBoxColumn"].Controls[0];
                chkbox.Enabled = true;
            }
        }

    If you are using GridTemplateColumn with CheckBox and in order to get reference to the row in which the CheckBox is checked, try the following code snippet in CheckedChanged event of CheckBox.

    ASPX:
    <telerik:GridTemplateColumn>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Eval("isapproved") %>' AutoPostBack="True"
                OnCheckedChanged="CheckBox1_CheckedChanged" />
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
       {
         CheckBox chkbox = (CheckBox)sender;
         GridDataItem item = (GridDataItem)chkbox.NamingContainer;// accessing corresponding row
       }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top