How to Find Control id Telerik Radgrid

2 posts, 0 answers
  1. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

    Posted 19 Feb 2015 Link to this post

    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#
  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    833 posts

    Posted 23 Feb 2015 Link to this post

    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.

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