简体   繁体   English

在ASP.NET用户控件中启用异步回调

[英]Enabling asynchronous callbacks in asp.net user controls

What is the ideal way to enable asynchronous... Ajax-like callbacks in a asp.net user control. 什么是启用异步... asp.net用户控件中的类似于Ajax的回调的理想方法。 The current design has an asp.net page using multiple user controls with intermittent post-backs and page reloads. 当前的设计具有一个asp.net页面,该页面使用多个用户控件以及间歇的回发和页面重新加载。

I am trying to incorporate a no page reload experience without the usage of JavaScript. 我正在尝试在不使用JavaScript的情况下合并无页面重装体验。 Is this possible? 这可能吗?

If not, how should I go about incorporating a no page reload unified experience in this scenario? 如果没有,在这种情况下我应该如何合并无页面重新加载统一体验?

There's no way to avoid JavaScript altogether. 无法完全避免使用JavaScript。 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. 您可以使用诸如UpdatePanel类的东西,因此您不必自己编写代码,但是JavaScript仍在幕后使用。

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. 一般来说,ASP.NET不能真正没有JavaScript来运行,因为除按钮外,其他所有控件都使用__doPostBack JavaScript函数进行回发。

a postback does it 回发

EDIT 编辑

The most harmonious way to ajaxify your server control is probably to wrap it in an UpdatePanel : 合并服务器控件的最和谐方法可能是将其包装在UpdatePanel

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <!-- your server control here -->
    </ContentTemplate>
</asp:UpdatePanel>

There are in fact, two possible solutions to your problem: 实际上,您的问题有两种可能的解决方案:

Update Panel 更新面板

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. 到目前为止,您已经提到了许多可以将现有控件包装在UpdatePanel (链接)中的方法,但这仍然会使面板的内容回传到服务器上,但是迄今为止,最方便,最快的方法是使用最大引人注目的 ,可用性提高。

Async Callbacks 异步回调

Using the Async Callback in .NET (link) you can, with a lot more control send data to and from your javascript files; 使用.NET (链接)中的异步回调,您可以通过更多控制权来往与javascript文件之间发送数据;

You can execute server side methods by calling a specific javascript function that you register on the start page: 您可以通过调用在起始页上注册的特定javascript函数来执行服务器端方法:

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: 这是通过实现ICallbackEventHandler接口来实现的,该接口为您提供了两种方法,即:

RaiseCallbackEvent : The event that is called when you send data from your javascript client and contains an argument variable. RaiseCallbackEvent :当您从javascript客户端发送数据时调用的事件,其中包含一个参数变量。 (With the use of JSON this single variable obsticle is easily overcome.) (通过使用JSON可以轻松克服单个变量对象。)

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. GetCallbackResult :完成服务器端方法后,您可以以字符串形式发送结果,其中包含您可能需要返回的内容。

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. 可以将这两种方法结合起来,并在回发服务器控件(网格,下拉列表等)和检查某些条件(服务器端)之间找到中间值,而不必回发仅查找未满足的某些条件。

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/ 如果您正在寻找一种无需编写任何自定义JavaScript即可合并“不重新加载”体验的快速方法,则可以查看ASP UpdatePanel http://ajax.net-tutorials.com/controls/updatepanel-control/

It's simple to setup, and handles 'everything' for you. 它很容易设置,并为您处理“一切”。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM