Showing posts with label postback. Show all posts
Showing posts with label postback. Show all posts

Monday, March 26, 2012

Determining the source of a partial rendering postback

Is there a way to determine (on the server) what trigger/control/event was the source of the postback? How about something that indicates what update panel(s) will be rendered?

I see that the script manager has an IsInAsyncPostBack property, but I'm curious if there is something more specific.

UpdatePanel.IsInPartialRendering does not seem to work.

You could check the _EventTarget for a hint on what triggered the PostBack.

What would you do differently if you knew what triggered the PostBack?


You could use the ScriptManager.AsyncPostBackSourceElementID to determine the source of the asynchronous postback. In terms of what UpdatePanels will get updated, in server code I don't think there is a summary list that lists them because the Update() method can be called by any code and tracking that is difficult if not impossible(?). However, on the client you can use the pageLoaded or pageLoading events of the PageRequestManager to get information about what panels will be created, updated or deleted.

Determine what element caused the Postback when handling the endRequest event

Hi All,

I have an UpdatePanel with 2 buttons. Pressing either of them causes a Postback and the panel gets refreshed.

I'm handling the endRequest event on the client side. Is there a way to determine which of the page elements caused the Postback?

Thanks,

Uzi

Try here:

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


Hi,

you can determine the postback element on the client side with the PageRequestManager. Attach to the beginRequest event and you can save the postback control. More information here:

http://ajax.asp.net/docs/ClientReference/Sys.WebForms/BeginRequestEventArgsClass/default.aspx

Regards
Marc André


Here is a neat way to get it done:

In the 'BeginRequest' event handler insert the postback element to the UserContext of the WebRequest:

function BeginRequestHandler(sender, args)

{

var elemName = args.get_postBackElement().id;var request = args.get_request();

request.set_userContext(elemName);

}

Then, when the request ends, at the 'EndEvent' get the information from the Response object:

function EndRequestHandler(sender, args)

{

var response = args.get_response();

alert(response._webRequest.get_userContext());

}

Thanks,

Uzi

Detecting UpdatePanel postback Vs regular postback

Probably a stupid question...

Is it possible to detect on the server-side whether or not the request is a full post back or an updatepanel postback?

Reason is I have a couple of controls on the page that are rather database-intensive and if since an UpdatePanel postback causes a full page cycle, if I could detect it I could avoid having those problem doing the db-intensive rendering task.

Thanks!

Ben

Hi Ben,

You can ask the scriptmanager if it's in a partial page postback, see my example in VB

If ScriptManager.GetCurrent(Me).IsInAsyncPostBack()Then'is in partial postbackEnd If


EACH CONTROL has its own events. if you bind the other controls in the page load event, enclose them with:

if not page.ispostback then

end if

the UpdatePanel has its own events where you can work on.


hi,

i don't think there is any method to identify which postback is done. According to my observation both postbacks are same


First to Check if it is AsyncPostback you can use the YourScriptManager.IsAsyncPostBack and to detect which control casued the postback use the YourScriptManager.AsyncPostBackSourceElementID.


Thanks Richard, that worked just fine (I knew that had to be simple!), marked your reply as answer.

Thanks all for your replies too.

Cheers,

Ben

Detecting UpdatePanel postback so can prevent onbeforeunload from firing

Hi, I was wondering if any of you AJAX gurus could help me out or point me in the right direction...

I'm using onbeforeunload on a form that has UpdatePanels. Anyone know how to detect the postback is an AJAX partial page update rather than a full page postback so I can prevent the onbeforeunload dialog showing in such cases?

Cheers.

No worries, I've just found this thread that gave me the info I needed to resolve my issue -http://forums.asp.net/p/1153470/1887704.aspx

detecting partial page postback

I read it somewhere here before, but can;t seem to find the reference today.

What is the code for separating the different types of postbacks (full page, partial postback, etc.)? I need to do this prammatically from a user control.

Hi,

You have ScriptManager.IsInAsyncPostBack to check if the page is performing an asyncpostback and UpdatePanel.IsInPartialRendering to check whether that particular updatepanel is performing a partial rendering.

Hope this helps,

Juan


Thanks for the info, that helps alot.

For anyone out there doing this from within a usercontrol, here is some code:

Dim tempScriptAs ScriptManager

tempScript =CType(page.FindControl("scriptManager1"), ScriptManager)

Dim asyncAsBoolean = tempScript.IsInAsyncPostBack

--

<

asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="true">

</asp:ScriptManager>

Detect if a request is a partial postbak?

Hello,

is it possible to check if the current request is a partial postback or not?


I am getting PageRequestManagerParserErrorExceptions when i use a response filter that does a gzip compression in global.asax and my idea was to detect if the current request is a partial one. I that case i would not apply the filter.


The only question is if it is possible to detect the difference between normal and partial postbacks.

Any hints?

Ciao,

Ralf

ScriptManager.IsInAsyncPostBack.

Thanks!

My only problem is now that the process of applying the filter happens in global.asax and i have to find out how to access my scriptmanager instance (scriptmanager is contained in the masterpage) from within global.asax.


OK, i have added the scriptmanager to the application now so i cann access it from within global.asax but: When performing pagings in a gridview that is contained in an updatepanel the ScriptManager.IsAsyncPostback shows "false" (i have defined the paging events of the gridview as triggers so i expected to see IsAsyncPostback=true)

Is the IsAsyncPostBack property only valid at page level (i mean not useable in global.asax)?


I'm not sure, I'll defer to your experiences as an indicator that it's a page / instance level variable.

The request itself gets modified by the runtime, though, so that if you have an http reuqest fired by your updatepanel, it should have the additional http header of:

x-microsoftajax: Delta=true.

If you check the Request.Headers collection, that should be a way for your module to do the detection you're lookign for.

Hope that helps.

Paul


Hi Paul,

Yes, that helps! Now i can apply gzip/deflate filtersand use updatepanels without getting PageRequestManagerParserErrorExceptions by simply not using the filter on partial postbacks.

Thanks for the hint,

Ralf


Detect AJAX postback in Application.EndRequest

Anyone know if there's a way to detect whether the current request is an AJAX postback or a regular postback from inside the Application.EndRequest event of an HttpModule? I have a module that writes out information to the end of a normal request (using response.write). However, this causes an AJAX postback request to fail. Or maybe there's a better way to append some text to the end of a request than response.write?

Ideas?

Test for (Request["X-MicrosoftAjax"] == "Delta=true"). That request header is present in partial postbacks.


Most excellent. Thanks!

HOWEVER, I'm a bit PEEVED at Microsoft for making the client-side AJAX javascript so sensitive to the data that comes back. If any part of my code does a response.write anywhere, the stupid AJAX Javascript on the client goes wobble-kneed and dies. Sheesh! I'd really like to be able to write out some text (using response.write) in both the master-page AND the httpModule's end_request event, but the AJAX Javascript on the client won't allow it! What a pain.

I've seen some c# code on the internet that shows how to modify the response that AJAX returns, but it's confusing as it's doing text translation (i.e., the function of the code is getting in the way of me understanding what it's doing to the response string).

Thanks for the answer to the original question.


Use some sort of HTTP viewer to inspect a partial postback response, and you'll probably see a way to do what you want to. Once you see it, hands-on, it's not too difficult to figure out how to alter (or see why Response.Write kills it).

I use FireBug for that, myself. I think Fiddler is a comparable IE addon.

Wednesday, March 21, 2012

Declarative Postbacks in Beta 2

Hello,

I had a small script function to perform a declarative postback. This worked in the July CTP, and we are in the process of upgrading to Beta 2. It is erroring out on not being able to locate the Sys.WebForms.PostBackAction type. Has this been removed/moved? If so, could someone point me in the direction of where it is hiding?

function postbackFromJS(sender, e) {
var postBack = new Sys.WebForms.PostBackAction();
postBack.set_target(sender);
postBack.set_eventArgument(e);
postBack.performAction();
}

Thanks!

I looked around the PageRequestManager class and couldn't find much so it looks like it was removed, though not sure if it was renamed. The docs are lacking in this area.

Anyways, here's the class you can take a look around:http://ajax.asp.net/docs/ClientReference/Sys.WebForms/PageRequestManagerClass/default.aspx


hello.

the postbackaction is defined on the preview.js file (ie, in the value-added bits).