public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Posts Tagged ‘ASP.NET’

Misconception Regarding ViewState in ASP.NET

Posted by Virendra Dugar on April 16, 2009

Common misconception that ViewState holds values for controls such as TextBoxes.

Most ASP.NET developers think that the ASP.NET ViewState is responsible for holding the values of controls such as TextBox so that they are retained even after postback. But this is not the case.

Let’s understand with an example.

Place a web server TextBox control, an HTML server text box control (normal HTML input box with runat=server), a normal HTML input control(without runat=server), and a web server Label control.

Set the Text/Value property to “Initial Text” for the three TextBoxes, and set the Text property of the Label control to “Initial Label”.

Set the EnableViewState property to false for the first text box and the last controls (Label).

Now place two Button controls and set their text as “Change Label’s Text” and “Postback to Server”. On the button click event handler of the first button, write the following code:

private void btnChangeLabel_Click(object sender, System.EventArgs e)
{
   lblTest.Text = "Label's Text Changed";
}

There is no code on the second button’s Click event. It just submits the page to the server.

Now run this application. You can see the initial texts in the controls as you have set.

Now, change the text in all TextBoxes and set them to “Changed Text”. Now click the Post to Server button. What happens is that the first two textboxes retain their values, in spite of the ViewState property being set to false. But the last textbox, which is a simple HTML input control, loses the modified value when the page is reloaded.

Most developers would have expected all three textbox controls to lose their modified values (“Changed Text”), and after page re-loading, they expect “Initial Value” being written on all textboxes as we had disabled the ViewState.

The reason for this behavior is that ViewState is not responsible for storing the modified values for controls such as TextBoxes, dropdowns, CheckBoxList etc., i.e., those controls which inherit from the IPostBackDataHandler interface. After Page_Init(), there is an event known as LoadViewState, in which the Page class loads values from the hidden__VIEWSTATE from the field for those controls (e.g., Label) whose ViewState is enabled. Then the LoadPostBackData event fires, in which the Page class loads the values of those controls which inherit from the IPostBackDataHandler interface, (e.g., TextBox) from the HTTP POST headers.

Now, start the application again, and this time, click the “Change Label’s Text” button. When the page reloads, you will see that the programmatic change (made by our code in the event handler of the button’s Click event) was lost, i.e., we don’t see the Label’s text changed to “Label’s Text Changed”. Instead, we see the initial value of the Label again. This is because the Label control does not inherit from the IPostBackDataHandler interface. So the ViewState is responsible for persisting its value across postbacks, and since it has been disabled, the Label loses its value after clicking the “Change Label’s Text” button.

Now enable the ViewState for the Label control, and you can see the modified value (“Label’s Text Changed”) after clicking the same button.

So we conclude that controls which inherit from the IPostBackDataHandler interface will retain their values even if the ViewState has been disabled, as their values are stored in HTTP POST headers.

Click here to view the original version by Vivek Thakur.

Enjoy….

Advertisements

Posted in ASP.NET | Tagged: | 2 Comments »

SessionID changes with every request in the asp.net 2.0 application

Posted by Virendra Dugar on April 9, 2009

For any web programmer, its obvious to think and believe that SessionId remains same through out the user session and it was right till asp.net1.1. But in asp.net2.0, this behavior has changed. In the asp.net application new sessionid is returned with the response to every request until session objects are not used.

According to MSDN the reason/solution is:

“When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application’s Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.”

Click here to view the article.

Enojy…

Posted in ASP.NET | Tagged: | 1 Comment »

Exploring session in ASP.NET

Posted by Virendra Dugar on April 3, 2009

This article describe about session in ASP.Net 2.0. It also explains about different types of session, their configuaration.

Enjoy…..

Posted in ASP.NET | Tagged: | Leave a Comment »

Update Panel, Default Button and FireFox

Posted by Virendra Dugar on March 29, 2009

ASP.Net 2.0 comes up with a new concept called “Default Button” which is one button on the form will work as default button. Whenever we press the enter button default button will be called.

As for example: In a text box after typing you press enter your default button’s event will be fired.

However, the one thing that was bothering me with the Default Button is it worked like a charm in IE. In Firefox, it behaved weirdly. The problem wasn’t that it didn’t do a postback. That it did. What it didn’t do was hit the server side event (the button click event).

When you are selected in any textbox and you press enter, IE will submit this and hit the button click even. However with FireFox, it will only submit – not call the onclick event.

To solve this add: UseSubmitBehavior="False" to your submit button. Now you can press enter whenever you want in your form and it will do the proper submit regardless of IE or Firefox.

Enjoy….

Posted in Ajax, ASP.NET | Tagged: , | 4 Comments »

showModalDialog and postbacks in ASP.NET

Posted by Virendra Dugar on March 23, 2009

I learned something new today that when window.showModalDialog is used to open a modal window on the client from parent page using  javascript. And whenever postback occurs in the modal window, it would open up a new window.  A normal window.open works fine, this is the problem only with modal window.

Finally I found a solution which is very simple by just including the below tag in the <head> section of the modal Window.

<base target="_self"/>

 And also we need to set the output cache property to “1” otherwise next time when you try to open the modal window, it will take it from cache.

<% @ OutputCache Duration="1" VaryByParam="none"%>

Enjoy..

Posted in ASP.NET, JavaScript | Tagged: , | 4 Comments »

Visual studio keyboard shortcuts

Posted by Virendra Dugar on March 10, 2009

Download the cool PDF versions of all the keyboard shortcut available in Visual Studio 2005 and 2008.

For C#
For VB.NET

Make habit of using shortcut keys and save your time. 🙂

Enjoy…

Posted in ASP.NET, C#, VB.NET | Tagged: , , | Leave a Comment »

ASP.NET Page Life Cycle Overview

Posted by Virendra Dugar on March 6, 2009

I found some good articles of ASP.NET Page Life Cycle. Check it out.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

http://www.techinterviews.com/?p=176

http://www.sql-server-performance.com/articles/asp_ado/page_life_cycle_p1.aspx

There is a diagram of the page life cycle events available on these sites

http://www.eggheadcafe.com/articles/20051227.asp

http://spazzarama.wordpress.com/2009/02/04/aspnet-lifecycle/

Enojy…

Posted in ASP.NET | Tagged: | Leave a Comment »

ViewState and Readonly Property of text box.

Posted by Virendra Dugar on March 1, 2009

Well, this has been under discussion for some time that ASP.Net TextBox which has the Readonly property assigned true not retaining the server side or client side changes. The changes are getting ignored across postbacks.  

Let’s understand both the scenario

When EnableViewState is true

When EnableViewState is set to true, then readonly textbox does maintain the server side changes but it does not maintain client side changes.

Let us understand with an example 

Declare a readonly text box and two buttons

 

Put this javascript function in the head section which modifies the text box value on the client side.

In the page load section, set the text box value.

On button click, just display the value of text box using Response.write().

Now, Just execute this program.

Initially text box is having “Read only text box” . Now click on the Client Side button and you will see that text box value is changed to “Modified sample Text”. Now click on server side button. You will see the Server side value assigned to the text box is displayed on the screen not the client side value.

When EnableViewState is False

When EnableViewState is set to false, then readonly textbox does not maintain the server side changes and client side changes.

Let us understand with an example

Declare a readonly text box with EnableViewState= false and two buttons.

Put this javascript function in the head section which modifies the text box value on the client side.

In the page load section,set the text box value.

On button click, just display the value of text box using Response.write().

Now, Just execute this program.

 Initially text box is having “Read only text box” . Now click on the Client Side button and you will see that text box value is changed to “Modified sample Text”. Now click on server side button. You will see that nothing is displayed on the screen. Textbox loses not only the client side value but also the server side value assigned to it. 

The interesting thing is if you assign any value to the readonly text box with enableviewstate = false at design time, it maintains that value.

 For example,

Textbox3 maintains it’s value assigned at design time.

Now, the question over here is that is there any way to retrieve the value from the server once it’s lost. Yes there is..

Request.Form[TextBox2.UniqueID], This line will give the last modified (Whether on server or on client) value of text box.

As we saw,there are number of concerns while using the readonly property. The best solution to avoid all these issues is in the code behind file, within the Page_Load add the following line of code:-

Now you will notice that when you run the page, the client side changes you make in the TextBox (via the Javascript) is retained across postback.

Check out these other reference :

http://west-wind.com/weblog/posts/3939.aspx

http://www.g9th.com/blog/post/2006/12/Readonly-property-of-Textbox-in-ASPNET.aspx

Enjoy…..

Posted in ASP.NET | Tagged: | 8 Comments »

Capture button’s click event of user control in the page.

Posted by Virendra Dugar on February 22, 2009

Hi,

Yesterday one of my colleagues was facing a problem with button placed in user control. He wanted to capture button’s click placed in user control inside the page that holds the user control.

I made a sample application for him. I thought I should share with you also.

Download Sample Code.

This is my user control (.ascx) file. As you can see I have placed a button and onclick event is also defined. (See image.)

User Control

If you want to extend the button click placed in the user control to the page, then declare an Event handler and on the click of the button just make a call to the declared event handler.  This will work as a property for any web page. See below image.

User Control

Place the control in your .aspx page. In the Page_Load event of your page, assign a new event handler to the User control’s event handler property. And, now use the newly declared event handler.

Web Page

So, now when button (which is placed in user control) is clicked, event handler declared in the page will be used to handle the click event.

Enjoy……

Posted in ASP.NET | Tagged: , | 4 Comments »

ASP.NET Questionnaire

Posted by Virendra Dugar on February 17, 2009

Hi All,

I found some pretty good stuff of ASP.NET Questionnaire. Check it….

 

Enjoy…………….

Posted in ASP.NET | Tagged: | Leave a Comment »

 
%d bloggers like this: