Rajeshkumar
Rajeshkumar

Reputation: 75

Fetching control name inside html table in asp.net

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

Upvotes: 0

Views: 322

Answers (1)

CoolArchTek
CoolArchTek

Reputation: 3839

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

Upvotes: 1

Related Questions