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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 20
    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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT