I came across the needs to save certain state to the web server using Ajax when a user navigates away from a page, so i tried to use the browser (IE8) "unload" event to do it.
One thing that i noticed when using the "unload" event is that, once the user clicks on a new link to navigate to a new page, browser will first send the request to web server causing the "pageLoad" function to be triggered then only the Ajax request is process in the web server.
If the next page that the user navigates to relies on the first page state, there will be problem because the new page is loading without getting the data from previous page.
My workaround is not to rely on the "unload" event, but to use user action to save the state. For example, when the user clicks on a link to do popup, after the popup immediately save the state to server. It's costly but working for now.
Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts
Friday, March 25, 2011
Tip: JavaScript "unload" event myth for IE8
Wednesday, March 2, 2011
Tip: Asp.Net Request.InputStream is empty?
Recently I needed to post some json data from browser to web server. I used the following codes to read the input stream:
System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
There's one problem with the above codes, that is the json variable will hold empty string even though I am sure that data is posted to the web server. The resolution of the problem is to always set the stream position to 0. The resulting codes looks like this:
Request.InputStream.Position = 0; System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
Now with the first line of the codes, you will be able to get the stream data.
System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
There's one problem with the above codes, that is the json variable will hold empty string even though I am sure that data is posted to the web server. The resolution of the problem is to always set the stream position to 0. The resulting codes looks like this:
Request.InputStream.Position = 0; System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
Now with the first line of the codes, you will be able to get the stream data.
Tuesday, December 30, 2008
Tip: CalendarExtender In Update Panel with ReadOnly TextBox
Scenario:
From time to time we want to build a date selection in a web form using the Ajax Control Toolkit's Calender Extender and to set a text box that accepts a new date everytime a user chooses a new date to ReadOnly mode so that user cannot modify the text box with unintended date format. Most of the time, we want to put it inside an UpdatePanel so that only partial of the page is updated. The problem is, the TextBox's TextChanged event if you enable it, it will not fire if the user makes a date selection and changes the content of the text box.
Solution:
The solution to this problem is to bind the UpdatePanel "Load" event to a function. In this case, any changes in the text box content will trigger the UpdatePanel event.
In the function what we can do is to check the "__EVENTTARGET" returned from the "Request" object. For example, the following code checks whether the postback is caused by the myTextBox text box.
protected void UpdatePanel2_Load(object sender, EventArgs e)
{
if (Request.Params["__EVENTTARGET"] == myTextBox.ID)
{
//DO something
}
}
Subscribe to:
Posts (Atom)