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

How to Find Control id Telerik Radgrid

1 Answer 699 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jaya
Top achievements
Rank 1
Jaya asked on 19 Feb 2015, 08:15 AM
Hi
I have Bind My TelerikRadgrid Label, checkbox , Textbox etc some controls 

here when i click button click event i need how to find Telerik radgrid controltype name for ex:

How to find Label control id and Checkbox id and Textbox id how will do this C#

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 23 Feb 2015, 12:05 PM
Hello,

The following code snippet shows how to get the type and the ID of a TextBox control in your page:

ASPX
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>


Code-Behind
protected void Button1_Click(object sender, EventArgs e)
{
    string typeName = Page.FindControl("TextBox1").GetType().Name;
 
    var textBoxes = Page.GetAllControlsOfType<TextBox>();
    foreach (var tb in textBoxes)
    {
        var id = tb.ID;
    }
}

FindControls static class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public static class FindControls
{
    public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control
    {
        var result = new List<T>();
        foreach (Control control in parent.Controls)
        {
            if (control is T)
            {
                result.Add((T)control);
            }
            if (control.HasControls())
            {
                result.AddRange(control.GetAllControlsOfType<T>());
            }
        }
        return result;
    }
}

I am not sure whether you are trying to get the control ID or the control type so you can see how to do both in Button1 Click event handler:
  1. You know the control ID and want to find its type. You can see how you can get it and store it in the variable typeName.  In our example it holds the type of the control with an ID "TextBox1".
  2. You know the type of the control and you want to get its ID. We use the static method GetAllControlsOfType defined in the static class FindControls to get the controls with a specific type. In our case we use it to get all TextBox controls and store the collection in textBoxes. You can then iterate over this collection, access the ID property of each control of the specified type that it holds and apply your custom logic.

Regards,
Ivan Danchev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jaya
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or