ASP.NET
  Home arrow ASP.NET arrow Page 3 - ASP.NET Custom Server Controls: Cute Image...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Custom Server Controls: Cute Image Hover Button
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 22
    2005-09-19

    Table of Contents:
  • ASP.NET Custom Server Controls: Cute Image Hover Button
  • Understanding the life cycle – Rendering methods
  • Understanding the life cycle – Attributes
  • Other features of the control

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys...the next control is on its way...don't hesitate to ask for enhancements...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek