Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Render method.

Render method.

Scheduled Pinned Locked Moved ASP.NET
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1804931
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • U User 1804931

      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

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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[^]

      U 1 Reply Last reply
      0
      • M minhpc_bk

        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[^]

        U Offline
        U Offline
        User 1804931
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • U User 1804931

          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

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • M minhpc_bk

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • L Lost User

              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

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              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[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups