Reputation: 5090
What is the ideal way to enable asynchronous... Ajax-like callbacks in a asp.net user control. The current design has an asp.net page using multiple user controls with intermittent post-backs and page reloads.
I am trying to incorporate a no page reload experience without the usage of JavaScript. Is this possible?
If not, how should I go about incorporating a no page reload unified experience in this scenario?
Upvotes: 0
Views: 5619
Reputation: 7117
There are in fact, two possible solutions to your problem:
As many by now have mentioned you could wrap your existing controls in a an UpdatePanel
(link) This still however causes the content of the panel to post back to the server but is by far the easiest and quickest to implement with the biggest, noticeable, usability improvement.
Using the Async Callback in .NET (link) you can, with a lot more control send data to and from your javascript files;
You can execute server side methods by calling a specific javascript function that you register on the start page:
void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cm = Page.ClientScript;
String cbReference = cm.GetCallbackEventReference(this, "arg","ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
cm.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);
}
This is hooked up by implementing the ICallbackEventHandler
Interface giving you two methods, namely:
RaiseCallbackEvent
: The event that is called when you send data from your javascript client and contains an argument variable. (With the use of JSON
this single variable obsticle is easily overcome.)
GetCallbackResult
: Upon the completion of your server side method you can send a result in form of a string, containing anything you may require back to your page.
This is extremely suited for long running processes returning a confirmation or checking simple conditions before posting back completely and will make a huge usability improvement.
Combining these two methods is possible and finding a median between posting server controls back (Grids, Dropdowns etc) and checking certain conditions (server side) without posting back only to find some conditions have not been met.
Upvotes: 3
Reputation: 46057
There's no way to avoid JavaScript altogether. You can use something like an UpdatePanel
so you don't have to write the code yourself, but JavaScript is still being used behind the scenes.
ASP.NET in general can't really function without JavaScript, because aside from buttons, every other control does a postback uses the __doPostBack
JavaScript function.
a postback does it
EDIT
The most harmonious way to ajaxify your server control is probably to wrap it in an UpdatePanel
:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!-- your server control here -->
</ContentTemplate>
</asp:UpdatePanel>
Upvotes: 3
Reputation: 8280
If you're looking for a quick way to incorporate a "no reload" experience, without writing any custom JavaScript, you could look at the ASP UpdatePanel http://ajax.net-tutorials.com/controls/updatepanel-control/
It's simple to setup, and handles 'everything' for you. :)
Upvotes: 1