Render method.
-
Hi, Is it possible to add a usercontrol to the page in onPreRender() event. I am trying to add a user control in onPreRender event but the control is adding after the tag. I want the control to be first control in the page.I don’t want to register the control. In the same way I want to add user control before the . I want to keep this functionality in one routine so it can be used for all the webpages. Is it possible to manipulate the writer object which is passed as a parameter to the render method. Regards. Chakrvarthy.V
-
Hi, Is it possible to add a usercontrol to the page in onPreRender() event. I am trying to add a user control in onPreRender event but the control is adding after the tag. I want the control to be first control in the page.I don’t want to register the control. In the same way I want to add user control before the . I want to keep this functionality in one routine so it can be used for all the webpages. Is it possible to manipulate the writer object which is passed as a parameter to the render method. Regards. Chakrvarthy.V
Hi there, You can add a user control to the page in the Page_PreRender event, however, if you need to process something like postback events of the user control, then this choice is not a good practise. You normally use the Add method to dynamically add a control, and this method will add the user control to the end position of the Controls collection. If you want it to be the first child control, you can use the AddAt method which takes two parameters and the first one is the location value in the collection. However, adding the user control to a position other than at the end may have some problem with the ViewState, so you need to consider carefully. What do you mean by manipulating the writer object which is passed to the Render method? Basically, this object is of the HtmlTextWriter or Html32TextWriter type depending on the client browser, and it provides a set of utility methods which you can use to create the appreance of the control. With these methods, there are two common ways to write the html elements to the output. You either directly write the html elements by using the Write methods or render tags, attributes and finally write them as a unit with the RenderEndTag method. For more information, you can see: ControlCollection Class [^] HtmlTextWriter Class[^] Rendering an ASP.NET Server Control[^]
-
Hi there, You can add a user control to the page in the Page_PreRender event, however, if you need to process something like postback events of the user control, then this choice is not a good practise. You normally use the Add method to dynamically add a control, and this method will add the user control to the end position of the Controls collection. If you want it to be the first child control, you can use the AddAt method which takes two parameters and the first one is the location value in the collection. However, adding the user control to a position other than at the end may have some problem with the ViewState, so you need to consider carefully. What do you mean by manipulating the writer object which is passed to the Render method? Basically, this object is of the HtmlTextWriter or Html32TextWriter type depending on the client browser, and it provides a set of utility methods which you can use to create the appreance of the control. With these methods, there are two common ways to write the html elements to the output. You either directly write the html elements by using the Write methods or render tags, attributes and finally write them as a unit with the RenderEndTag method. For more information, you can see: ControlCollection Class [^] HtmlTextWriter Class[^] Rendering an ASP.NET Server Control[^]
I also did the same thing, but I got the following exception. Control '_ctl0_DropDownList1' of type 'DropDownList' must be placed inside a form tag with runat=server. In my UserControl I have one DropDownList box. Can you please tell me why this error is occurred? Regards, Cahakravarthy.V
-
I also did the same thing, but I got the following exception. Control '_ctl0_DropDownList1' of type 'DropDownList' must be placed inside a form tag with runat=server. In my UserControl I have one DropDownList box. Can you please tell me why this error is occurred? Regards, Cahakravarthy.V
Because you use the code
this.Controls.Add()
in code-behind of the page to add a user control, then it is added to the page while there might be some controls declared in your user control requiring to be put in the server form element. So you need to use the Form element in your code instead of the Page object. In code-behind, you simply put the declaration for the form element:...
//I assume that the form element's id is Form1.
protected System.Web.UI.HtmlControls.HtmlForm Form1;
...Now, you can use the Form1 object to add a user control to its Controls collection.
-
Because you use the code
this.Controls.Add()
in code-behind of the page to add a user control, then it is added to the page while there might be some controls declared in your user control requiring to be put in the server form element. So you need to use the Form element in your code instead of the Page object. In code-behind, you simply put the declaration for the form element:...
//I assume that the form element's id is Form1.
protected System.Web.UI.HtmlControls.HtmlForm Form1;
...Now, you can use the Form1 object to add a user control to its Controls collection.
-
Hi, Now it is working fine, can you please explain me why it is working after the change? I didn't understand the behavior. Where I will get the JavaScript samples which are written in C#, Asp.Net Regards, Chakravarthy.v
Hi there, The Page object at runtime has 3 items in its Controls collection:
+[0]: System.Web.UI.ResourceBasedLiteralControl, contains the markup from the top of the page to the end of the opening body tag.
+[1]: System.Web.UI.HtmlControls.HtmlForm, contains the form element.
+[2]: System.Web.UI.LiteralControl, contains the markup from the closing body tag to the end of the page.If you add a user control with code this.Controls.Add method, your control will be placed at the end of the array (after the LiteralControl). Meanwhile, there's a dropdownlist control declared in the user control, and the dropdownlist needs to always be in the server form tag. So you need to add the user control to the Controls collection of the form element, but not the Page object. To look for examples with javascript, you simply google for it. Clickety[^]