Help on User controls ... ascx
Posted on 2005-04-25
I am aware of when and why to use user control .. advantages/disadvantages .. difference between user controls and web custom controls etc etc ..
I have created a user control which contains a DHTML menu and some textboxes. I have a basepage which is inherited by all my other pages within the project. As the menu has to be placed on the top of almost all of these pages, what I have done is
1. dynamically add the control in the load event of the basepage. When I added the usercontrol using loadcontrol, it gave the error that the controls have to be within the form tags with runat=server (as there are other controls like textbox within the usercontrol) .. So I used a findcontrol of the formId (page.FindControl("Form1")) and added the usercontrol to the first index of controls using "AddAt" .. But then this logic requires that all my pages which inherits the basepage name the form as "Form1" which is what is been used by the BasePage ... I do not want to use a placeholder as then this would mean that I will have to add the placeholder in all the Pages which inherits the BasePage .. Help needed here on any other approach ...
2. Expose a property (Boolean .. default is true) within the usercontrol .. this is used by the basepage to decide whether the usercontrol has to be shown in the current page or not (CurrentPage inherits basepage) .. this is set in the load of the current page. Now the usercontrol is loaded in load event of the basepage .. So if i set this property within the load event of the CurrentPage, then it is not set within the usercontrol as the load of the basepage gets run first .. So should the usercontrol be added in the init of the basepage? If yes, is this the correct approach or just a workaround??
3. Now how will I access the controls within the usercontrol (the names are generated as _ct10_txtTextBoxName) .. would these names ever change in subsequent addition of more controls to the usercontrol or compilations .. what is the correct method/logic to acheive all this .. Help needed here too .. links to any tutorials examples which explain these concepts is appreciated .. I did search for examples and most of the articles talks about adding usercontrol to the page during design time and most of the usercontrols do not have any other controls within it (seen examples of usercontrol menus using DHTML) ..
Hope this discourse hasn't been very lengthy and hope you had the patience to reach the end and help me out here ..
Thanks a lot in advance