2013-10-01 48 views
0
控制的类型

我的设计如下:如何获得使用AssociatedControlID

<asp:Label ID="lbl1" runat="server" AssociatedControlID="ddl1"> 
</asp:Label> 
<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList> 

这样我有几个标签,我想找出与每个标签相关的控制型我的形成。是否有可能获得控制类型?

回答

0

您可以使用FindControl并传入AssociatedControlID

Control c = FindControl(lbl1.AssociatedControlID); 
if(c == null) // Not found 
else 
{ 
    Type t = c.GetType(); // Gets the type of the control 
    if(c is TextBox) // I'm a textbox 
    else if(c is DropDownList) // I'm a DropdownList 
} 
+0

嗨,我们可以得到哪些控制是通过使用这种 – Vivekh

+1

@Vivekh,检查使用switch语句与DropDownlist,TextBox等进行比较 –

0

在后面的代码试试这个:

foreach (Label lbl in this.Page.Form.Controls.OfType<Label>()) 
{ 

}