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.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?