Telerik RadGrid – Accessing Controls In Edit Mode

In this post, I’m going to talk a little bit about how to access specific controls within a RadGrid while that grid is in edit mode. There are several reasons you may want to do this:

  • You might need to set some defaults for the control.
  • You want to perform some calculation and output some default value to the control.
  • You may need to change the attributes of a control (color, font etc).

In order to achieve any of the above, we need a way to get access to the controls while in edit mode.

To begin, you’ll need to add the ItemDataBound event to your grid, so that when we’re in edit mode and the items are being bound, we can perform our custom code.

1
OnItemDataBound="RadGrid1_ItemDataBound"

Next, add the method to handle the ItemDataBound event for the grid as follows:

1
2
3
4
5
6
7
8
9
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        TextBox txtbx = (TextBox)edititem["ColumnUniqueName"].Controls[0];
        txtbx.Text = "";
    }
}

The above code is pretty straight forward. We check first to see if we’re in edit mode, because we don’t want to be performing this code during any other time. We create a GridEditableItem object so that we can talk to the controls of the item we’re currently editing. And finally, if it’s a textbox we’re dealing with, create a TextBox object and using the unique name you’ve assigned that column, we can access the control within it.

Now that you have a TextBox object, you can access all the properties and methods of that object as you normally would.

If the control in question is a checkbox, it’s not much different:

1
2
3
4
5
CheckBox cb = (CheckBox)edititem["ColumnUniqueName"].Controls[0];
 
if (cb.Checked) {
    // perform some custom code
}

5 Responses to “Telerik RadGrid – Accessing Controls In Edit Mode”

  1. ozgu November 1, 2011 at 2:40 am #

    I Know its old post but in (TextBox)edititem[“ColumnUniqueName”].Controls[0]; line Controls Count is 0 so it is throwing OutofRange exception. Any idea?

  2. shereen November 18, 2011 at 3:58 pm #

    Hi ozgu,

    So what type of field is this? Can you post or email shereen at qumsieh.ca your entire page so I can have a look. It might be easier to spot your issue that way.

  3. Kumar March 18, 2015 at 7:02 am #

    Hi

    Regarding Running Total in TelerikRadGrid in Footer Template

  4. Meenu November 2, 2015 at 2:02 pm #

    Can anyone tell me ,, I have a checkbox in Edittemplate and want to check the value and post the same to Database. Its working fine but when I am trying to add new record its showing error. Below is my code…

    <asp:CheckBox ID="chkActive" runat="server" Checked='’ Visible=”/>

    <asp:CheckBox ID="chkActive" runat="server" Visible='’ Checked=”false” AutoPostBack=”true” />

  5. Gilles Leblanc January 27, 2016 at 3:03 pm #

    Thanks! I couldn’t find how to do this on Telerik’s website.

Leave a Reply