ASP.NET
  Home arrow ASP.NET arrow Page 4 - ASP.NET Custom Server Controls: Cute ASP.N...
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 ASP.NET TextBox Control
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2005-08-30

    Table of Contents:
  • ASP.NET Custom Server Controls: Cute ASP.NET TextBox Control
  • Developing the “MyTextBox” Control
  • Testing the control with a Web Application
  • Understanding the MyTextBox
  • Handling the “PostBack” from “ViewState”
  • Is it cute? How can we make it cute?

  • 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 ASP.NET TextBox Control - Understanding the MyTextBox


    (Page 4 of 6 )


    In this section, we shall go into the details of the “MyTextBox” custom control. First of all let us consider the following line:


    Public Class MyTextBox

    Inherits System.Web.UI.WebControls.WebControl

    Implements IPostBackDataHandler


    I already explained that any custom control (or class of custom control) needs to be derived either from System.Web.UI.Control or System.Web.UI.WebControls.WebControl. The above statement does exactly this. Until there we are all right. But what is the use of “IPostBackDataHandler”? This is the interface we need to implement to support the feature of “postback.” We shall see its details later. Let us proceed to the following code fragment:


    Property [Text]() As String

    Get

    Return viewstate("text") & ""

    End Get


    Set(ByVal Value As String)

    viewstate("text") = Value

    End Set

    End Property


    I hope you can understand the above. I define a property named “text,” which has the “viewstate” support. Any information we store in “viewstate” would exist to the server even after further web requests from the same page in a magical manner.

    All of the information available in “viewstate” will be encoded and added to a hidden field to the same page by the ASP.NET runtime. When a post back occurs, all the information in that hidden field again gets posted to the server along with new pairs of values. We then check (or compare) the previous values (or information in viewstate) with recently modified values by the user and modify the view state accordingly during a “postback” event. This could be observed better from the next section.

    Continuing with the above code, we have:


    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

    MyBase.AddAttributesToRender(output)

    output.AddAttribute (HtmlTextWriterAttribute.Name, Me.UniqueID)

    output.AddAttribute (HtmlTextWriterAttribute.Id, Me.ClientID)

    output.AddAttribute (HtmlTextWriterAttribute.Type, "text")

    output.AddAttribute (HtmlTextWriterAttribute.Value, Text)

    output.RenderBeginTag (HtmlTextWriterTag.Input)

    output.RenderEndTag()


    End Sub


    The above code fragment is at the heart of displaying the “textbox.” First of all, I add all the attributes of the “input” tag such as name, id, type, value, and so forth using the “output.AddAttribute” method. “UniqueID” and “ClientID” are automatically generated when a control has been dragged from the toolbox on to the web form.

    “MyBase.AddAttributesToRender(output)” is the statement which is essential to making it available as the first statement. All of the default attributes, such as “style” (what we set at design time using the properties window) will be added using that simple statement. If you forget that statement, you will be left with a very plain textbox without having any of your design time properties set.

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Communicate with your network of business associates, agents, dealers and clients...
       · Is your post, having any relation with my article? I hope this is not an...
       · Hello guys, this is my article on creating a Cute ASP.NET textbox control. You can...
     

    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 6 hosted by Hostway
    Stay green...Green IT