GregD Posted September 30, 2004 Posted September 30, 2004 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! Quote
wessamzeidan Posted October 1, 2004 Posted October 1, 2004 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 Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
GregD Posted October 1, 2004 Author Posted October 1, 2004 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 Quote
bri189a Posted October 1, 2004 Posted October 1, 2004 You could use Page.Controls and recursively enumerate through all the controls that way. You shouldn't use CType either: http://www.xtremedotnettalk.com/showthread.php?t=80277&highlight=directcast+ctype Also (from MSDN): ...the run-time performance of DirectCast is better than that of CType. Quote
GregD Posted October 1, 2004 Author Posted October 1, 2004 (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 October 1, 2004 by GregD Quote
GregD Posted October 1, 2004 Author Posted October 1, 2004 (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 October 1, 2004 by GregD Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.