Showing posts with label regular. Show all posts
Showing posts with label regular. Show all posts

Monday, March 26, 2012

How do I distinguish between AJAX postback and regular postback?

I just installed Ajax 1.0 with VS2005 SP1 and am trying to add AJAX to an existing site.


However, the site right now has fairly complex code in the master page defined in Page_init, Page_load, and PreRender events. I'd really like to skip those calls if it is an AJAX post back. I looked at the Page class and found IsCallBack and IsPostBack, both of which will not be triggered by an AJAX post back.

I thought of using a hidden variable (something like AJAXCall) and using a Javascript to set a value with every control that can trigger an AJAX postback, but that seems like not the right way to do it.

Does anyone know how to tell the difference between the two, or is it possible to skip Page_init, Page_load, and PreRender events with an AJAX call?

Thanks,

Ming

Yes you can, but you have to use UpdatePanel.

Girijesh:

Yes you can, but you have to use UpdatePanel.

I have tried the following setups with no success. In the master page, I included my script manager. Then I had an update panel with a button which updates a label with the system time. This takes about 10 seconds to update. The debugger tells me it executes Page_Load, Page_Init and PreRender.

Then I tried to include an update panel with a button inside page that inherits the master page, it also goes into all 3 methods. I tried this with my existing website, as well as a brand new AJAX enabled website.

Any ideas?

Thanks,


Ming


Hi Ming,
You can use ScriptManager.IsInAsyncPostBack property to distinguish them.
Hope this helps.

Raymond Wen - MSFT:

Hi Ming,
You can use ScriptManager.IsInAsyncPostBack property to distinguish them.
Hope this helps.

It worked like a charm. Thanks for telling me exactly what I was looking for.

Thanks!

Wednesday, March 21, 2012

How can I use a email regular expression in Ajax Toolkit

Hi I am developing an application using the validations of AJAX Toolkit. In one screen i make the user input the email address. I want to have a regular expression which will validate the input done by user. How can i add a regular expression in Ajax Toolkit.

Early response will be appreciated.

Praveen Mishra

drag a regularexpression validator on your form & set its controltovalidate property and regularexpression property. The client side validations will trigger before the partial postback.

Cheers

Vishal Khanna

(pls mark as answer if reply helps)


See the last bullet on this page:http://blogs.visoftinc.com/archive/2007/09/23/asp.net-ajax--updatepanel-not-working--common-problems.aspx about using validators with an UpdatePanel (assuming this is what you mean by the "Ajax Toolkit")

To use the the ValidatorCallout with your RegularExpressionValidator, seehttp://www.asp.net/AJAX/AjaxControlToolkit/Samples/ValidatorCallout/ValidatorCallout.aspx

-Damien


You have to set ValidationExpression Property of RegularExpressionValidator.control

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.validationexpression.aspx

How can I tell the difference between an AJAX and a regular postback in the Page_Load even

Is there anyway to determine if the page is called through an AJAX or a regular postback in the Page_Load method, kinda like you can determine if it is a postback or the initial request via the Page.IsPostBack property?

I hoped the Page.IsAsync could be used for this but it doesn't seem to work like I expected.

I think when an AJAX does a post back you cannot access any controls like Label's, buttons etc...so try accessing a label by going label.text, if it fails catch the Exception...not a good way to do it, but a cheaky option for a quick test.


if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

this can differentiate those methods.


What you're after is ScriptManager.IsInAsyncPostBack.


Sathesh_pandian:

if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

Test that.

You'll find that Page.IsPostBack is actually true during partial postbacks.


That's it. ScriptManager.IsInAsyncPostBack does the trick. Thank you.Big Smile


it will not work when we are using it for different page.

calling another page.


That's because that isn't a postback.