locked
overriding Render on different Controls RRS feed

  • Question

  • User1942128046 posted

    Hi all,

    I was wondering what makes controls render in different ways. I'll explain an expample.

    When creating a custom web control, if I inherit from WebControl, and I add a child control in code by doing; "this.Controls.Add(myControl)", I do not have to do anything else for the child control to appear. What I mean is that by just adding that line of code it will display it when it renders.

    However, If I inherit from a Button, I specifically have to call the RenderControl or Render ( I do not know the difference between these two methods to be honest) when overriding the Render. For innstance:

    public class MyCustomButton : Button
    {

       Button btnConfirm = new Button();
    
       protected override void Render(System.Web.UI.HtmlTextWriter writer)
       {
          btnConfirm.RenderControl(writer);
          base.Render(writer);
       }
    }
    I'm confused... 
    Many Thanks
    Friday, July 4, 2008 8:39 AM

Answers

  • User-16411453 posted

    This is the standard template I use for the majority of my web custom controls.  Sub Render Renders and Paints all of the controls. Sub OnInit Initializes the controls objects, and handles the postback and button clicks. RenderDesignMode makes a serperate version for use in Design Mode.

     
     
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Drawing.Design
    Imports System.Web.UI.Design
    Imports AjaxControlToolkit
    Imports System.Security.Cryptography
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.Xml.Serialization
    Imports System.IO
     
     <DefaultProperty("Text"), ToolboxData("<{0}:Display_FedEx_RateCertification runat=server></{0}:Display_FedEx_RateCertification>")> _
    Public Class Display_FedEx_RateCertification Inherits WebControl
    Private Panel As Panel
    Private UpdatePanel_FedEx_RateCertification As UpdatePanel
    Private UpdateProgress_FedEx_RateCertification As UpdateProgress
    
    Private lblStatus As Label
     
    	  
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
    	If Not Me.Context Is Nothing Then
    		RenderContents(writer) //Render Run Time
    	Else
    		RenderDesignMode(writer) //Render Design Mode
    	End If
    
    End Sub 
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    	MyBase.OnInit(e)
    
    	Controls.Clear()
    
    	Dim cs As ClientScriptManager = Me.Page.ClientScript
    	Dim rsType As Type = Me.GetType()
    
    	Dim Comment As LiteralControl
    	Comment = New LiteralControl
    	Comment.Text = "<!-- Internet Commerce Engine 5 Display_FedEx_Registration Control V1.3 for ASP.NET -->" & vbCrLf
    	Controls.Add(Comment)		
    	UpdatePanel_FedEx_Registration = New UpdatePanel		
    	UpdatePanel_FedEx_Registration.ID = [ID] & "_UpdatePanel_FedEx_FedEx_Registration"		
    	UpdatePanel_FedEx_Registration.ChildrenAsTriggers = True		
    	UpdatePanel_FedEx_Registration.UpdateMode = UpdatePanelUpdateMode.Conditional		
    	Panel.Controls.Add(UpdatePanel_FedEx_Registration)
     
    End Sub  
    Private Sub RenderDesignMode(ByVal writer As HtmlTextWriter)
    
    	Controls.Clear()
    
    	Dim cs As ClientScriptManager = Me.Page.ClientScript
    	Dim rsType As Type = Me.GetType()
    
    	Dim Comment As LiteralControl
    	Comment = New LiteralControl
    	Comment.Text = "<!-- Internet Commerce Engine 5 Display_FedEx_Registration V1.3 for ASP.NET -->" & vbCrLf
    	Controls.Add(Comment)
    
    	Panel = New Panel
    	Panel.Attributes.Add("id", [ID] & "_FedEx_Registration")
    	Panel.Visible = [Visible]
    	Panel.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString & "px")
    	Panel.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    	Panel.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    	Controls.Add(Panel)
    
    	Panel.RenderControl(writer)
    
    
    End Sub  
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    	MyBase.OnLoad(e)
    
    	If Not Page.IsPostBack Then
    		Dim rv As Boolean = False
    		rv = DoesXMLExist()
    		If rv = False Then
    			MakeInitialXML()
    			panelInitialize()
    			LoadData()
    		Else
    			panelInitialize()
    			LoadData()
    			LoadRegisterCSP()
    			LoadSubscriberService()
    			LoadVersionCapture()
    		End If
    	End If
    
    
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 5, 2008 12:57 PM
  • User481221548 posted

    Hi there

    Every Render-Call is a - so called - "mass render call" if the Control and owner of the render-Method have Child-Controls.
    So "Button" is not a Container Control, so the internal logic doesn`t care about Child-Controls.

    So the only Method to add some additional Markup to a Extended Button-Control is to override the Render Method or another involved Method in the Render-Cycle.

    > I've read I must inherit from CompositeControl... Would this do a mass render or a specific render? Is the any other advantage of using CompositeControl, as inheriting from WebControl also allows me to add child controls so..
    No, the CompositeControl Baseclass is "only" offering a new API to make a Compositum of Controls.
    You can make a Compositum as well with any Class that derives from "Control"...

    IMO the normal way with the Control or WebControl and not the CompositeControl Baseclass is the better and easier one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 7:02 PM

All replies

  • User-16411453 posted

     One is a mass render call, and the other is the controls render call.  I suspect the mass render call triggers each objects individual render call to render or paint your controls to the screen.  But just because you painted the control to the screen, doesn't mean you'll get full functionality out of the control, or will it retain values or states.

    Friday, July 4, 2008 4:13 PM
  • User1942128046 posted

    Ahhh that would make sense!

    So when adding child controls, I've read I must inherit from CompositeControl... Would this do a mass render or a specific render? Is the any other advantage of using CompositeControl, as inheriting from WebControl also allows me to add child controls so...

    Thanks again for the help.

    Saturday, July 5, 2008 7:32 AM
  • User-16411453 posted

    This is the standard template I use for the majority of my web custom controls.  Sub Render Renders and Paints all of the controls. Sub OnInit Initializes the controls objects, and handles the postback and button clicks. RenderDesignMode makes a serperate version for use in Design Mode.

     
     
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Drawing.Design
    Imports System.Web.UI.Design
    Imports AjaxControlToolkit
    Imports System.Security.Cryptography
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.Xml.Serialization
    Imports System.IO
     
     <DefaultProperty("Text"), ToolboxData("<{0}:Display_FedEx_RateCertification runat=server></{0}:Display_FedEx_RateCertification>")> _
    Public Class Display_FedEx_RateCertification Inherits WebControl
    Private Panel As Panel
    Private UpdatePanel_FedEx_RateCertification As UpdatePanel
    Private UpdateProgress_FedEx_RateCertification As UpdateProgress
    
    Private lblStatus As Label
     
    	  
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
    	If Not Me.Context Is Nothing Then
    		RenderContents(writer) //Render Run Time
    	Else
    		RenderDesignMode(writer) //Render Design Mode
    	End If
    
    End Sub 
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    	MyBase.OnInit(e)
    
    	Controls.Clear()
    
    	Dim cs As ClientScriptManager = Me.Page.ClientScript
    	Dim rsType As Type = Me.GetType()
    
    	Dim Comment As LiteralControl
    	Comment = New LiteralControl
    	Comment.Text = "<!-- Internet Commerce Engine 5 Display_FedEx_Registration Control V1.3 for ASP.NET -->" & vbCrLf
    	Controls.Add(Comment)		
    	UpdatePanel_FedEx_Registration = New UpdatePanel		
    	UpdatePanel_FedEx_Registration.ID = [ID] & "_UpdatePanel_FedEx_FedEx_Registration"		
    	UpdatePanel_FedEx_Registration.ChildrenAsTriggers = True		
    	UpdatePanel_FedEx_Registration.UpdateMode = UpdatePanelUpdateMode.Conditional		
    	Panel.Controls.Add(UpdatePanel_FedEx_Registration)
     
    End Sub  
    Private Sub RenderDesignMode(ByVal writer As HtmlTextWriter)
    
    	Controls.Clear()
    
    	Dim cs As ClientScriptManager = Me.Page.ClientScript
    	Dim rsType As Type = Me.GetType()
    
    	Dim Comment As LiteralControl
    	Comment = New LiteralControl
    	Comment.Text = "<!-- Internet Commerce Engine 5 Display_FedEx_Registration V1.3 for ASP.NET -->" & vbCrLf
    	Controls.Add(Comment)
    
    	Panel = New Panel
    	Panel.Attributes.Add("id", [ID] & "_FedEx_Registration")
    	Panel.Visible = [Visible]
    	Panel.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString & "px")
    	Panel.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    	Panel.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    	Controls.Add(Panel)
    
    	Panel.RenderControl(writer)
    
    
    End Sub  
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    	MyBase.OnLoad(e)
    
    	If Not Page.IsPostBack Then
    		Dim rv As Boolean = False
    		rv = DoesXMLExist()
    		If rv = False Then
    			MakeInitialXML()
    			panelInitialize()
    			LoadData()
    		Else
    			panelInitialize()
    			LoadData()
    			LoadRegisterCSP()
    			LoadSubscriberService()
    			LoadVersionCapture()
    		End If
    	End If
    
    
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 5, 2008 12:57 PM
  • User481221548 posted

    Hi there

    Every Render-Call is a - so called - "mass render call" if the Control and owner of the render-Method have Child-Controls.
    So "Button" is not a Container Control, so the internal logic doesn`t care about Child-Controls.

    So the only Method to add some additional Markup to a Extended Button-Control is to override the Render Method or another involved Method in the Render-Cycle.

    > I've read I must inherit from CompositeControl... Would this do a mass render or a specific render? Is the any other advantage of using CompositeControl, as inheriting from WebControl also allows me to add child controls so..
    No, the CompositeControl Baseclass is "only" offering a new API to make a Compositum of Controls.
    You can make a Compositum as well with any Class that derives from "Control"...

    IMO the normal way with the Control or WebControl and not the CompositeControl Baseclass is the better and easier one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 7:02 PM
  • User1942128046 posted

    Thanks for your replies. That clarified it completely.

     many thanks!

    Saturday, July 12, 2008 5:27 AM