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

Need help implementing a checkbox

1 Answer 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
toddhd
Top achievements
Rank 1
toddhd asked on 07 Dec 2010, 10:41 PM
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.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Dec 2010, 07:08 AM
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.
Tags
Grid
Asked by
toddhd
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or