ASP.NET Custom Server Controls: Cute Image Hover Button - Understanding the life cycle – Attributes
(Page 3 of 4 )
In the previous section, we saw the methods implemented for rendering the control. But, we didn’t talk much about the “AddAttributesToRender” method. In this section, we will deal with it.
The “AddAttributesToRender” method is basically used (or overridden) to add HTML attributes and styles that need to be rendered for the respective custom control. Let me explain its implementation to you part by part (available in the downloadable solution).
MyBase.AddAttributesToRender(writer)
writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID)
writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0px")
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0px")
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px")
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "'url(" & ImageOutURL & ")'")
writer.AddStyleAttribute("cursor", "hand")
writer.AddStyleAttribute("text-Align", "center")
writer.AddStyleAttribute("vertical-Align", "middle")
If you observe carefully, you will see that within the above set of statements, the first statement is “base.AddAttributesToRender(writer)”. Is this necessary? Of course it is! It is always a good practice to have the statement “Base.AddAttributesToRender(Writer)” as the first statement within the “AddAttributesToRender” method. Never remove it (unless you have any reason to do so). There also exists one more secret behind it.
During the design time of VS.NET, you may move the control to your favorite location (or adjust its size, or change some of its properties, and so on). All of that information (say CSS) would be remembered and again written back to our control using this statement. If you forget this statement, your control works in a floating manner at the left-top of the web page (without having any design time attributes set).
The statement “writer.AddAttribute” is generally used to emit an attribute of a tag. You can use all existing attributes from “HtmlTextWriterAttribute” or simply provide your own string in quotations. The second statement (writer.AddStyleAttribute) is generally used to add style (or inline CSS) to a tag. You can use all existing styles from “HtmlTextWriterStyle” or simply provide your own string in quotations. I hope you can understand the rest. Proceeding further we have:
If Not Page.IsStartupScriptRegistered(Me.UniqueID & "_MouseOver") Then
Page.RegisterStartupScript(Me.UniqueID & "_MouseOver",
getJS4MouseOver(Me.UniqueID & "_MouseOver"))
End If
If Not Page.IsStartupScriptRegistered(Me.UniqueID & "_MouseOut") Then
Page.RegisterStartupScript(Me.UniqueID & "_MouseOut", getJS4MouseOut
(Me.UniqueID & "_MouseOut"))
End If
writer.AddAttribute("onMouseOver", Me.UniqueID & "_MouseOver();")
writer.AddAttribute("onMouseOut", Me.UniqueID & "_MouseOut();")
The above statements actually register JavaScripts to the aspx page. I am always using “UniqueID” in the above statements because the JavaScript for each instance of this control should be different for each one. If we don’t use this trick, all controls would focus on the same JavaScript, resulting in very funny outputs! Okay. Let us proceed further.
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(Me, String.Empty))
The above statement is the most important (or even the heart) of all, as it emits the JavaScript for an “auto-postback” to the server when the control is clicked. Without the above statement, it would never behave like a button.
Next: Other features of the control >>
More ASP.NET Articles
More By Jagadish Chaterjee