0

Here is my sample code

for (int i = 0; i< sdtable1.rows.count ; i++) {

System.Web.UI.Control ctr = SDTable1.Rows[i].Cells[0].Controls[i];

StudentDetailsChecklist.Items.Add(ctr.ID);

}

SDTable is my Html table, I want to fetch the control names not Id.

If I use the above code it fetches the ID of the controls, I need to fetch the name of the control.

Thanks Rajeshkumar

Rajeshkumar
  • 75
  • 1
  • 2
  • 10
  • You can do it easily using jquery – Kiranramchandran Sep 30 '13 at 14:39
  • Please show us where you get stack rather than we try to guess. – Win Sep 30 '13 at 14:49
  • I am just looking for the code friends. – Rajeshkumar Sep 30 '13 at 14:57
  • 2
    @Rajeshkumar There are multiple ways to achieve it, but it all depends on how you create your html table. Please show us code how you create the html table. – Win Sep 30 '13 at 15:02
  • Its a static table with static controls. I have to take the control name and pass it to the listbox. Its my scenario. – Rajeshkumar Sep 30 '13 at 15:08
  • @Rajeshkumar: It always helps to see your markup. That way we don't have to pepper you with questions such as: Does the table (and the controls) have `runat='server'`. Is it inside of another control - like an `asp:panel`? Is it created via ajax calls or through a web user control or is it directly on the aspx page? etc. In other words, show the code you have and someone will happily let you know what to do. – NotMe Sep 30 '13 at 21:29
  • for (int i = 0; i < SDTable1.Rows.Count; i++) { System.Web.UI.Control ctr = SDTable1.Rows[i].Cells[0].Controls[i];}. I like to fetch the name of the control stored in "ctr" instance. – Rajeshkumar Oct 01 '13 at 06:36

1 Answers1

1

It boils down to enumerating all the controls in the control hierarchy:

IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

You can use it like this:

    foreach (Control c in EnumerateControlsRecursive(Page))
    {
        if(c is TextBox)
        {
            // do something useful
        }
    }

Source

Community
  • 1
  • 1
CoolArchTek
  • 3,729
  • 12
  • 47
  • 76