Put TagName of user control into variable.

GregD

Newcomer
Joined
Jun 26, 2004
Messages
15
How can I grab the tag names of all the user controls currently loaded on a page... the tag names are specified by the @Register command at the top of the page like so:

Code:
<%@ Register TagPrefix="CMS" TagName="LoginControl" Src="login.ascx" %>
<%@ Register TagPrefix="CMS" TagName="ModSecDesc" Src="secdesc.ascx" %>

So for this page I want to get LoginControl and ModSecDesc into 2 different variables. Im pretty sure its the system.web.ui.UserControlControlBuilder class that I should get the property from but how do I do it?

Thanks!
 
I don't know why you need the tagname. If you want to access properties/methods of the usercontrols, you can use either of these 2 ways.

1. Declare a variable of the same type and same name of your usercontrol
eg. Dim Login1 As New Login

2.You can use the FindControl method to get a reference to your usercontrol, but you have to cast it to the proper type
eg. Ctype(Me.FindControl("Login1"),Login).Visible=True
 
wessamzeidan said:
I don't know why you need the tagname. If you want to access properties/methods of the usercontrols, you can use either of these 2 ways.

1. Declare a variable of the same type and same name of your usercontrol
eg. Dim Login1 As New Login

2.You can use the FindControl method to get a reference to your usercontrol, but you have to cast it to the proper type
eg. Ctype(Me.FindControl("Login1"),Login).Visible=True

Thanks! Yep quite right...

How would I find all the user control ID's on a page for the class ModSecDesc ? i.e. Ctype(Me.FindControl("*"),ModSecDesc)

Obviously it wouldnt be * but i need to get all the ids without actually knowing them if you see what i mean. I had a look at the page class but could only find Page.ClientID which only works if you know the ID.

Thanks
 
I tried using the following code which I found on http://aspnet.4guysfromrolla.com/articles/082102-1.aspx

Code:
void IterateThroughChildren(Control parent)
{
  foreach (Control c in parent.Controls)
  {
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
          && c.ID == null)
    {
      // ...do something...
    }
        
    if (c.Controls.Count > 0)
    {          
      IterateThroughChildren(c);          
    }
  }
}

But it only iterates through the child controls not the parents i.e. my custom user controls. I tried using the user control class instead of just the control class but it doesn't work. Any ideas?

Im trying to get all the IDs for the control type ModSecDesc:

Code:
<CMS:ModSecDesc id="blah" runat="server"/>
<CMS:ModSecDesc id="moo" runat="server"/>
 
Last edited:
Worked it out but damm was not easy at all...

Code:
Sub WalkControls(ByVal parent As Control, ByVal level As Integer)

Dim ctrl As Control
For Each ctrl In parent.Controls
If ctrl.GetType().BaseType.Tostring = "CMS.ModSecDesc"
Response.Write("<b> " & ctrl.ID & "</b> " & ctrl.GetType().FullName & "<br>")
End If
        ' **** Recurse into this item's Controls collection if it's not empty
If ctrl.Controls.Count > 0 Then
WalkControls(ctrl, level + 1)
End If

And in Page_Load:

Code:
WalkControls(Page, 1)

Found it here and adapted it a bit using basetype:

http://www.dotnet247.com/247reference/msgs/5/29593.aspx
 
Last edited:
Back
Top