简体   繁体   English

在asp.net的html表中获取控件名称

[英]Fetching control name inside html table in asp.net

Here is my sample code 这是我的示例代码

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

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

StudentDetailsChecklist.Items.Add(ctr.ID); StudentDetailsChecklist.Items.Add(ctr.ID);

} }

SDTable is my Html table, I want to fetch the control names not Id. SDTable是我的HTML表格,我想获取控件名称而不是ID。

If I use the above code it fetches the ID of the controls, I need to fetch the name of the control. 如果使用上面的代码,它将获取控件的ID,则需要获取控件的名称。

Thanks Rajeshkumar 感谢Rajeshkumar

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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM