Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

<%@ 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!

Posted

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

Proudly a Palestinian

Microsoft ASP.NET MVP

My Blog: wessamzeidan.net

Posted
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

Posted (edited)

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

 

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:

 

<CMS:ModSecDesc id="blah" runat="server"/>
<CMS:ModSecDesc id="moo" runat="server"/>

Edited by GregD
Posted (edited)

Worked it out but damm was not easy at all...

 

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:

 

WalkControls(Page, 1)

 

Found it here and adapted it a bit using basetype:

 

http://www.dotnet247.com/247reference/msgs/5/29593.aspx

Edited by GregD

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...