How to post with update panel and re-rout its response data - asp.net

I implemented a save-draft trick using an update panel whereby I handle the updatepanel's asynch postback on the server side and then to avoid resending the same html data I throw an exception with the current datetime as message and then capture it on the client side where i do some transformation and html injection to let the user know that a draft of his current work has been saved (TERRIBLE!).
The original idea was to handle the asynch postback and then override the rendering method of the update panel to send xml or javascript data that could be captured and processed on the client side stopping the update panels refresh from occuring.
Has anyone tried this kind of functionality using update panels?

I don't think that an UpdatePanel is really what you want here. If you're wanting to get the response back and have the best control over the response you should look at standard AJAX requests.
An UpdatePanel isn't something that you can really control the response of all that well, you can tie into the endRequest event handler on the PageRequestManager, there you can check the respose. Here's the details on the eventArgs you get back - http://msdn.microsoft.com/en-au/library/bb384175.aspx
But throwing an Exception isn't a good idea because nothing exceptional has happened, which is what an exception is ;)

Related

Difference between IsPostBack, IsCallback and IsCrossPagePostBack in ASP.NET

Can someone point me to a link which explains difference between IsPostBack, IsCallback and IsCrossPagePostBack in ASP.NET?
I have googled but didn’t get exact information.
Found below link for difference between IsPostBack, IsCallback.
What is the difference between Page.IsPostBack and Page.IsCallBack?
Thanks,
Balu
1) IsPostBack: "A postback is a request sent from a client to server from the same page, user is already working with." ASP.NET was introduced with a mechanism to post an HTTP POST request back to the same page. It’s basically posting a complete page back to server (i.e. sending all of its data) on same page. So, the whole page is refreshed.
2) IsCallBack: “A callback is generally a call for execution of a function after another function has completed.” But if we try to differentiate it from a postback then we can say: It’s a call made to the server to receive specific data instead of whole page refresh like a postback. In ASP.NET, its achieved using AJAX, that makes a call to server and updating a part of the page with specific data received.
3) IsCrossPagePostBack: “Gets a value indicating whether the page is involved in a cross-page postback or not." It's a different feature from "IsPostBack" and "IsCalBack" because It's generally used when we need to get the data from previous page
Reference Link

Disable Button During Postback With Server-Side Validation

Been through a few threads and wasn't able to find this situation exactly. My page is ASP.NET 4.5, C#. I use "Server Side" validation for (IMHO) security reasons and have several validation groups on a dynamic form.
I think we are all familiar with the advantages of disabling a button during Postback to prevent double clicking. I've read through quite a few threads with examples using OnClientClick in various ways.
What I'd like my page to do is something like this:
1 - User clicks Submit (or whatever button)
2 - Server-side custom validation fires on appropriate validation group
3 - if the validation passes, disable the client button(s) and proceed with Postback
4 - if the validation doesn't pass, show the error as usual. Client buttons should still be enabled.
Number 3 seems to be the kicker. Is this even possible with server-side validation? It seems out of order to validate server-side, come back to execute javascript, then back to postback. If it is possible, then I'm muddy on some of the mechanics. Specifically, how to tell the client that the specific validation group passed okay on the server side. Less specifically, the entire thing, lol.
Thanks!
Since you're using ASP.NET, I'm assuming you're also using jQuery (which is the default).
$('#MyForm').submit(function (event){
event.preventDefault();
var form = $(this);
if (!form.data('submit-lock')){// check if we're waiting on the server's response
form.data('submit-lock', true);// set the lock since we're about to post to the server
$.ajax(/*your url and data and whatnot here*/)
.always(function(){
form.data('submit-lock', false);// the server responded, unlock
});
}
});
$.ajax() returns a jQuery Deferred promise object.
promise.always(function) registers a function to call when the ajax is resolved (success) or rejected (fail).
More reading on the deferred pattern:
http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/

How does ASP.NET identify that the request is a postback?

I am a bit curious how ASP.Net internally identifies that the request is a postback.
I have read in a Microsoft book that you can technically do a postback using both POST and GET methods
This means that commands do not have anything to do with postback.
I have tried to use Fiddler to see what the request headers are sending. I am thinking that it could be something to do with the viewstate but I am not sure.
You're nearly right.
The correct event is fired based on the _EVENTTARGET and _EVENTARGUMENT variables which are sent as part of the request. I believe the IsPostBack is set based on the values of these. These determine which event to fire and with what arguments.
The actual submit is fired by the __doPostBack() function in javascript.
More detail here: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/05/18/Which-Control-Caused-PostBack_2100_.aspx

How does RequiredFieldValidator automatically stops database being updated?

My previous impression of RequiredFieldValidator and similar have been that they show you an error label and set Page.IsValid false and that is it. Meaning they leave the rest of the task (preventing the use of wrong input data) to you. But today I have realised say if you use a DetailsView to insert a new record to a database, and you use validators to check the TextBoxes inside the DetailsView, they automatically prevent the database from being updated.
I would like to know how this is implemented behind the scene. I'm guessing it aborted the Page Lifecycle at Validator.PreRender event, so that database connections at later stage could not be reached? I'm probably wrong.
I'm trying to use Reflector to get inside the RequiredFieldValidator to see how it is implemented, but I don't really know where to look. Can someone give some hints?
This article explains the validation in detail for ASP.NET.
http://msdn.microsoft.com/en-us/library/aa479045.aspx
Validation can be both Server side and/or client side. If used, client side validation doesn't let user submit the form until it is validated. Client side validation is implemented using JavaScript and DOM. Every submit button is wired by the framework to check validation before doing post back.
Server side validation works differently. The event chain is extended between page load and event procedure call. The validation results are set, which can be interpreted by any event procedure.
In your situation I guess you have client validation on - which it prevents Form from being submitted to server and that's why no actual update to database is done.
EDIT: DetailsView control does support server side validation controls. Follow this link for details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#security

Multiple update panels and multiple postbacks cause entire page to refresh

I'm having the same problem I had yesterday... The solution Aristos provided helped solve the problem, but I have other places sending updatepanel postbacks causing the same problem.
When an update panel is updated and another request for an update is called before it has a chance to render the first updates, the entire page refreshes instead of just the update panel.
I used fiddler to see what was going on and here's what happens... If I wait for the request to return before doing another request I get this:
21443|updatePanel|dnn_ctr1107_CRM_SalesTool_LeadsUpdatePanel|
But if I don't wait, I get this:
66|pageRedirect||http://mysite.com/salesdashboard.aspx|
The code from the previous question is still the same except I added UpdateMode="Conditional" to the update panel.
Any ideas? Or is the only solution to this making sure that 2+ updates for any number of update panels (as long as they're on the same page) never happen?
Thanks,
Matt
Maybe microsoft automatic ajax can not handle 2 request at the same time because he needs to know what is the post back every time, so probably if you click when he wait for a return, then he knows that the post back, has change, so he by pass ajax to be soure for correct return.
I can think 2 ways. One way is to make it by your self using jQuery and ajax and avoid UpdatePanel.
Second way, is to block the clicks when you wait for the return, or make a mechanism to place the request, the one after the other.
This code can help you know when you block the input and when you release it, or do what ever you think.
$(document).ready(function() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
});
function InitializeRequest(sender, args) {
LastIdCaller = args.get_postBackElement().id;
}
function EndRequest(sender, args) {
}
I could be wrong, but if I'm right then you can't do multiple ajax request if you are using asp.net WebForms. In asp.net WebForms there is only one Form element on a page. Multiple ajax requests on the same page require multiple form elements for each. Html is designed to post back inside a form element and the mechanism that handles said postback is the form, it's the container. Only one can post back at a time. So because Asp.Net WebForms only has one form element per page, you can only do 1 ajax postback per page.
Optionally, you can create generic ASHX Http Handlers to do your form logic and use JQuery Ajax to post back to the Generic Http Handlers. In which case you can do as many of those at a time as you want.
Generally I use ASHX handler's any time I need to serve images that change, like on the fly imaging. I also use them for large data outputs. E.g. the ASHX handler returns a big dump of JSON. I do an Ajax Postback to the ASHX handler to get a set of data in JSON and append it to a table etc and I call the ASHX handler repeatedly on a timer to get new Data as it comes in (say a 5 min timer) etc.
If you gave some more context on what your trying to do I might be able to provide you with alternative solutions.
Edit: I looked at your other post you linked and I think an ASHX handler would serve you well. You can design the ASHX handler to return your Search Data in JSON. You can use Request varaibles in the ASHX handler and you can send post data to the ASHX Handler with JQuery.Ajax.
You should be able to fire off multiple requests each with it's own Success Function. Then you would need to write the javascript in such a way that as it's processing the JSON data from the ashx handler it can merge with other requests as they finish.

Resources