ASP.NET Custom Server Controls: Cute ToolTip Control with Better Framework - How is it rendered?
(Page 3 of 6 )
In my previous examples, I defined “RenderBeginTag”, “Render”, “RenderEndTag” and several other methods of rendering. In this scenario, as the control (tooltip) will not be displayed immediately after the web page gets rendered, certain methods are not used.
Finally, I ended up overriding only two methods from the base class “webcontrol”. They are as follows:
- AddAttributesToRender
- Render
Even though it is not essential to use the “AddAttributesToRender” method in this case, I used it to separate the registration of JavaScript code from the “Render” method. It is defined as follows:
Protected Overrides Sub AddAttributesToRender(ByVal writer As
System.Web.UI.HtmlTextWriter)
MyBase.AddAttributesToRender(writer)
'emit javascript only at run-time
If Not IsDesignTime Then
If Not Page.IsStartupScriptRegistered
("ToolTipFunctions") Then
Page.RegisterStartupScript("ToolTipFunctions", getJS4ToolTip())
End If
End If
End Sub
Within the above method, I am trying to call the “MyBase.AddAttributesToRender(Writer)” statement to add any design-time attributes to my control. It is not essential for this control in this scenario, but this is a must when we design interactive controls (as explained in my previous articles of the same series).
And you can even observe that I am using the “IsDesignTime” method here. I would like to emit JavaScript code if, and only if, it is not design time in Visual Studio.NET. I think you can understand the rest. It's emitting JavaScript from a single function named “getJS4ToolTip” (which we will see in later sections). Coming to the “Render” method, it is defined as follows:
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
AddAttributesToRender(writer)
writer.Write("<iframe id=""frIFrame"" name=""frIFrame""
frameBorder=""0"" scrolling=""no"" ondrop=""return
false;""></iframe>")
End Sub
The above method gets executed immediately when the control has to render itself. The first statement tries to emit JavaScript code (if it is not design time). Immediately after that, I am emitting an embedded IFRAME (not at all enclosed in DIV as stated in my previous article). This emission of IFRAME is much simpler, when compared with the code available in my previous article for the same method.
Next: JavaScripts: from custom control >>
More ASP.NET Articles
More By Jagadish Chaterjee