home » articles » Story of two Textboxes

Story of two Textboxes

change text size: A A A

1/5/2009 by vivek_iit

 Once upon a time, there was a big city called ASP.NET where lived many varieties of controls who worked together to support rich modern web programming. In that city lived two friends, txtWebTextBox (son of rich System.Web.UI.WebControls.TextBox) and txtHtmlTextBox (son of poor System.Web.UI.HtmlControls.HtmlInputText).

txtHtmlTextBox belonged to an extremely poor family which did not have even bare necessities of life. But he worked hard in limiting resources and proved himself worthy of being set as “runat=server” attribute. RunAt server nomination was considered the highest among the folks of ASP.Net city and every one strived for it, though rich and powerful entities like those belonging to family of System.Web.UI.WebControls had the coveted attribute as default. And they never worried about it much.

So when txtHtmlTextBox was honoured with RunAtServer attribute, his family was extremely jubiliant as now he could work inside the main headquarters of the city and all of them felt proud. But this created a rift of jealousy between txtHtmlTextBox and txtWebTextBox, as the latter did not like the idea of working with the former whom he considered useless earlier. But as per the city’s laws, both had to work together with each other.

One day, txtWebTextBox was working on a highly important mission and felt the need of storing its important secret documents at a safe place. So he went straightway to the Bank of Viewstate and deposited all his papers with them so that he can get them at a later stage whenever he felt the need for them. He went to the clerk sitting there and asked him for a sign up form. The clerk told him to go to the Chairman of the bank, Mr. ViewState, for getting clearance. Mr.Viewstate was very happy to see txtWebTextBox and told him that he need not fill the form since as per the city laws, the family of System.Web.UI.WebControls.TextBox has an account in the bank by default, and they have “EnableViewState=true” for all the members of the family. txtWebTextBox was more than happy to know this. Further, Mr. ViewState told him that for the purpose of just storing his valuables, he need not use the services of the Bank of ViewState as he already belonged to the family of IPostBackDataHandler, which automatically does the work of saving the data even if you turn “EnableViewState=false”. Then Mr. Viewstate also cautioned him saying “..sometimes you will need our services if you want to store your properties such as ‘readOnly’, if they are changed at runtime due to some condition.””.

txtWebTextBox nodded his head and his eyes lit up listening to the privileges bestowed upon him. He rushed back to tell poor txtHtmlTextBox of his superiority and tease him.

He soon found txtHtmlTextBox working hard in the city headquarters. As he told him his new discovered powers with proud, txtHtmlTextBox just listened with a smile on his face. After txtWebTextBox finished, he shocked him by saying that he too can retain his valuables without needing the services of Mr. ViewState!

txtHtmlTextBox laughed at him thinking he was just bluffing. But he was in for a shock when txtHtmlTextBox told him that he also implements  IPostBackDataHandler interface and also has EnableViewState property.

txtWebTextBox was aghast. He could not believe that all such privileges which he thought were exclusive were shared by all. He rushed to his father and asked him what the difference between him and others.

His father told him not to think much as his family has many special privileges. txtHtmlTextBox has only 24 properties with him, while he has more than 40 properties, more than double, and also more event handlers and methods. So he is richer than txtHtmlTextBox. Also we have the power to detect the target browser's capabilities and render ourselves accordingly. And we web controls have purely object oriented approach from .Net point of view thus are more robust.

Also, my best friend HtmlButton (belonging to the family of HtmlInputButton class) implements IPostBackEventHandler interface, but it DOES not permit it to store its data across PostBacks unless it enables its viewstate.

“But son..”, he said “you need to be careful. Sometimes you do require the services of Mr ViewState.”

What does his father meant by saying that? 

See the code attached. Run it and type “a” in the second text box then press Click. You will see the value in first textbox is changed to World and it has become ReadOnly. Now remove “a” from the 2nd textbox and again click the button. The value is same in the first text box but it has lost its read only property as its viewstate was disabled. This won’t happen if you enable its viewstate.

Also, see the button below, it is simple html input control. But if you disable its ViewState, then it wont persist the changed value of its caption across post backs.

Moral of the story:

Be careful with Viewstate and IPostBackDataHandler. For controls such as textboxes, viewstate can be an extra burden.

To rate this article please register or login

Author

vivek_iit vivek_iit (Member since:11/27/2008)
I am one of the administrators at CodeAsp.Net and I love programming, architecting solutions, code reviews, teaching and writing about ASP.NET.

Comments (10)

  • Tim 12/24/2008 8:35:24 AM by:  Tim
    Great topic!
  • munya 1/14/2009 3:16:37 AM by:  munya
    Very good article, thanks a lot.
  • yogesh 2/11/2009 3:06:17 AM by:  yogesh
    Really Great
  • Vinz 4/21/2009 2:15:14 PM by:  Vinz
    Great post indeed! nice presented! :)
  • anilsharma 5/13/2009 11:00:57 PM by:  anilsharma
    Good story...... and u also good writer
  • batrapriyanka 5/16/2009 1:20:28 PM by:  batrapriyanka
    nice one!!!
  • 11/3/2009 6:25:40 AM by: 
    Great Article... very complex things explained in very simple manner
  • deepayan 11/9/2009 11:24:19 PM by:  deepayan
    nice story vivek ... u r quite a author too :)
  • silky.mca 11/28/2009 1:28:41 AM by:  silky.mca
    gud one...can u post sum more stories like dat
  • silky.mca 11/28/2009 1:29:16 AM by:  silky.mca
    gud one...can u post sum more stories like dat

Post a comment

Comment (No HTML)  

Type the characters:
 *
 
   

Related articles

Join CodeAsp.Net for FREE Today!

It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.

enter your email address:

Latest Articles RSS Feed

Latest Articles

  • Hi Friends, Many times we came cross the requirement when we need to access the dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.
  • This is the approach that I have adopted to develop Expandable / Collapsible Panel Control through JavaScript. Please report bugs, errors and suggestions to improve this control.
  • This is my approach to develop custom JavaScript ListBox control. Although it is only a subset of existing HTML ListBox element, it is more user friendly than the existing one. It can be further customized for different requirements. Please let me know about bugs and/or errors & give suggestions to improve this ListBox control.
  • I have tried my best to make this user control code error free. I will most welcome suggestions for further improvement in this user control. I have tested this user control on various browsers and it works fine.
  • So, this is my approach to implement an ASP.NET slide show using the DataList. I have tried my best to keep it bug free. I will most welcome suggestions and criticism for further improvements of this user control. I have tested this user control on various browsers and it works fine.
  • As we all knows that Repeater and DataList does not have auto paging support technique like Gridview or Datagrid, but we can achieve this through PagedDataSource. By using PagedDataSource we can implement paging in Repeater or DataList. Now in our mind there is question arise what is PagedDataSource. PagedDataSource is a class which encapsulates
  • So this is my approach. I was working for a long time to create C# like event handlers for JavaScript classes and finally, I’ve done it. Please let me know of any bugs and suggestions to improve this context menu control.
  • So this is my solution. If you have some other ideas about this functionality, please share them with me.
  • I have tried my best to make this code error free. Suggestions and criticism for further improvements of this code are most welcome.
  • So this is the approach that I've adopted to solve the Hover Delay problem. Although originally I developed Hover Delay to deactivate the click event for 1 second on a GridView row, later I also used Hover Delay to deactivate Drag n Drop of GridView rows. Kindly let me know if any one has some other better or different solution.
  • In this article I will explain how to import data from EXCEL to SQL in ASP.NET . In many situations we have data in the form of excel sheet but we have the requirement to have that data in SQL SERVER DB. I have explained importing data both from Excel 97-2003 as well as Excel 2007 format.
  • The source code shows how to use Regular Expressions in C#. The code Functions written for Validation Alphabet, AlphaNumeric, Integer, Postive Integer, Floating point numbers. You just cut copy these functions and use in any program.
  • That’s all about this technique. Just download the sample application and happy CSS! I have tested this application on various browsers and it worked fine.
  • This script is cross-browser compatible and fast as it iterates elements of a specific tag inside a target element [GridView] rather than iterating in a whole form. It searches the elements of a specific type in a particular column of the target element [GridView].
  • This Article is used to insert a numeric value on the sever control(text box) This is a java script code for the the client side validation. On Page Load Event You can change the events in txtNoOfQuestion.Attributes.Add("onkeypress", "return numericOnly(this);"); like onfocus events like other according to needs.
  • I have toggled visibility of all TD elements of a GridView column in order to create an illusion of smooth dynamic effect with the help of setTimeout method through recursion. Different browsers have different effects of Expanding / Collapsing GridView Columns. In Internet Explorer 7/8, Safari, Google Chrome and Opera, it seems that columns are Exp
  • In this article, I've used the setTimeout method in order to achieve a smooth expand/collapse functionality.
  • Introduction I am going to present here a functionality that selects / deselects all checkboxes of a particular column inside a GridView control, provided the header checkbox of that column is checked or unchecked using JavaScript. This functionality also has a feature that when all checkboxes of a particular column inside the GridView are check
  • This article describes how to toggle the states of all CheckBoxes inside a particular DataGridView column.
  • This article describes how to apply client-side mouse over & mouse out effects on the GridView’s rows.