在 jquery 选项卡下按需加载用户控件
我在用户控件上有几个 jquery 选项卡,每个选项卡下加载一个单独的用户控件。每个用户控件都是唯一的。目前一切正常,但整体页面响应太慢。为了提高性能,我尝试根据需要(即单击选项卡)在这些选项卡下加载一些用户控件。可能没有回发... ajaxish。 有人可以指导我吗? 我尝试遵循此教程和这个 也有一个但没有取得任何成功。我已附上父用户控件的代码。
<ul id="tabs">
<li class="active">Rewards</li>
<li id="liCoupons">Coupons</li>
<li id="liLibrary">Library</li>
<li id="liProducts">Favorite</li>
<li id="liPreferences">Preferences</li></ul><ul id="tabPanes" class="rewardsTabs">
<li>
<div class="wrapper active">
<uc:Rewards ID="wellness" runat="server" />
</div>
</li>
<li id="liCoupons">
<div class="wrapper">
<uc:Coupon runat="server" />
</div>
</li><li id="liLibrary">
<div class="wrapper">
<uc:Library runat="server" />
</div>
</li><li id="liProducts">
<div class="wrapper">
<uc:Products runat="server" />
</div>
</li>
<li>
<div class="wrapper">
<div class="preferences">
<uc:Preferences runat="server"/>
</div>
</div>
</li>
I have few jquery tabs on a usercontrol that loads a separate user control under each. Each user control is unique. It all works fine right now but the overall page response is too slow. In order to improve performance I am trying to load few user controls under these tabs on demand (that is on click of tab). Possibly without post back... ajaxish.
Can anyone guide me?
I tried to follow this tutorial and this one too but did not have any success. I have attached the code for parent usercontrol.
<ul id="tabs">
<li class="active">Rewards</li>
<li id="liCoupons">Coupons</li>
<li id="liLibrary">Library</li>
<li id="liProducts">Favorite</li>
<li id="liPreferences">Preferences</li></ul><ul id="tabPanes" class="rewardsTabs">
<li>
<div class="wrapper active">
<uc:Rewards ID="wellness" runat="server" />
</div>
</li>
<li id="liCoupons">
<div class="wrapper">
<uc:Coupon runat="server" />
</div>
</li><li id="liLibrary">
<div class="wrapper">
<uc:Library runat="server" />
</div>
</li><li id="liProducts">
<div class="wrapper">
<uc:Products runat="server" />
</div>
</li>
<li>
<div class="wrapper">
<div class="preferences">
<uc:Preferences runat="server"/>
</div>
</div>
</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
进行 ajax 调用
每次单击选项卡都会对 Web 方法
。每个方法将仅呈现所请求的控件。此外,在您的方法中,您可以根据需要保留或提取视图状态。渲染后,webmethod 应传回要注入占位符的 html 字符串。
如果您尝试过此操作并成功一次渲染一个控件,但仍然看到速度缓慢,那么您在获取数据时会遇到一些后端问题。如果您的控件数据量很大,我建议您进行一些服务器端缓存。
希望这有帮助。
The second link you mentioned should work. You don't need to define any user controls in your markup.
Each tab click will do an ajax call
to the web methods.
Each method will render only the requested control. Also in your method you can keep or extract the viewstate depending on your needs. After rendering, the webmethod should pass back the html string to be injected into the placeholders.
If you tried this and were successful rendering one control at a time but still seeing slowness then you have some back end issues while getting the data. If your controls are data heavy I would recommend doing some server side caching.
Hope this helps.
您的用户控件是否依赖回发和视图状态来工作?使用 AJAX 获取要在选项卡中显示的用户控件 HTML 相对容易,但随后在该控件上回发会将整个数据发送到实际页面(可能未加载用户控件)。因此,基本轮廓是
Does your user controls rely on post-backs and view-state for there working? It will be relative easy to fetch the user control HTML to be displayed in the tab using AJAX but then post-back on that control will send the entire data to the actual page (that may not have the user control loaded). So the basic outline would be
也许使用指向下面定义的服务的锚点。例如,
perhaps use an anchor that points to the service defined below. For instance,
您需要进行 Ajax 调用才能完成此操作。
现在您可以选择调用 AJAX:
1 - 通过 SOAP Web 服务调用(每个 Web 方法都需要 ASP AjaxScriptManager 引用)。
2- 通过 WCF 服务调用,如之前的答案。
3 - 通过休息服务呼叫。
4-通过 Jquery ajax 方法调用,但请求必须转到外部页面,如“Actions.aspx”,因此当您调用方法时,HTTPRequest 将被放入 Actions 页面,然后它将在其响应中包含返回的数据。 $.Ajax(url,数据,操作,successMethod); // 这是我尝试过的最快的方法。
这是你应该做的:
1- 在更改选项卡事件上,使用上述选项中适当的 Ajax 调用方法来调用您的方法。
2-从 success 方法中使用返回的数据,但最好对 DataTime 对象使用 eval(data) 。
下面是一些示例,解释了如何进行此调用:
现在在 Actions.ASPX 代码中使用以下内容:
You will need to make an Ajax call in order to make this.
now you have options to call AJAX:
1 - Call via SOAP web service (ASP AjaxScriptManager referencing will be needed for every web method).
2- Call via WCF Service as the previous answer.
3 - Call via Rest service.
4- Call via Jquery ajax method but the request must going to external page like "Actions.aspx" so when you call your method an HTTPRequest will be made into Actions page then it will have the returned data within its response.
$.Ajax(url,data,action,successMethod); // this is the fastest way I tried them all.
Here is what you should to do:
1- on the change tab event call your method by using the appropriate Ajax calling method from the above options.
2- from the success method use the returned data but it's better for you to use eval(data) for the DataTime objects.
here is some example explains how to make this call:
now at the Actions.ASPX code use the following:
如果你用 jquery 稍微调整一下,这应该可以工作:
http://blogs.msdn.com/b/sburke/archive/2007/06/13/how-to-make-tab-control-panels-load-on-demand.aspx
或者您只使用 asp.net 选项卡。
If you adjust it a little with jquery, this should work:
http://blogs.msdn.com/b/sburke/archive/2007/06/13/how-to-make-tab-control-panels-load-on-demand.aspx
Or you just use the asp.net tabs.
您应该使用 jquery 和 webmethod 访问第二个链接。这样,您实际上可以根据需要填充选项卡,而不会增加页面的负担。
You should go for the second link using jquery and webmethod. That way you will actually populate the tabs on demand without making you page heavy.
在我看来,解决问题的最快解决方案(但不一定是最好的长期解决方案)是将所有
UserControl
包装在 .aspx 页面中。在这种情况下,您只需将父UserControl
标记移动到新的 .aspx 页面,并通过 AJAX 调用它。假设您将此页面称为 Menu.aspx 之类的名称,并进一步假设您不需要传递到此页面的任何数据(即,它可以在内部跟踪其自己的所有数据),您的jQuery AJAX 调用看起来像这样:
一些注释:
done
和fail
将取代 jQuery 中的success
和error
1.8,所以任何 jQuery您使用的 AJAX 应该为这种转变做好计划。根据您对紧密耦合的 HTML/JavaScript 的感受,您可以将上面的
$placeholder
替换为回调函数。从菜单所在的页面调用它看起来像:有些人(包括我自己)使用
$
前缀来区分 JavaScript 和 jQuery 变量。所以这里,$placeholder
是一个 jQuery 变量。$.ajax
调用重写为type: "GET"
,这会更高效,但我不确定如果UserControl
会导致这方面的问题。通常,如果我通过 AJAX 加载整个 .aspx 页面,我会使用GET
而不是POST
。您无需进行太多更改即可尝试:只需切换type
属性并将result.d
更改为result
即可。In my opinion, the fastest solution to your problem (but not necessarily the best long-term) is to wrap all your
UserControl
s in a .aspx page. In this situation, you'd just have to move your parentUserControl
markup to a new .aspx page, and call it via AJAX.Assuming that you called this page something like Menu.aspx, and further assuming that you don't need any data passed into this page (that is, it can track all of its own data internally), your jQuery AJAX call would look something like this:
Some notes:
done
andfail
will replacesuccess
anderror
in jQuery 1.8, so any jQuery AJAX you use should plan for this transition.Depending on your feelings about tightly-coupled HTML/JavaScript, you could replace
$placeholder
above with a callback function. Calling that from your the page where your menu resides would look something like:Some people (myself included) use the
$
prefix to differentiate between JavaScript and jQuery variables. So here,$placeholder
is a jQuery variable.$.ajax
call as atype: "GET"
, which would be a little bit more efficient, but I'm not sure if theUserControl
s would cause problems in that regard. Normally, if I'm loading an entire .aspx page via AJAX, I useGET
instead ofPOST
. You don't have to change much to try it out: just switch out thetype
property and changeresult.d
toresult
.我认为最好的解决方案是实现客户端回调
http://msdn.microsoft。 com/en-us/library/ms178210.aspx
当用户单击某个选项卡时,onclick 事件以选项卡名称作为参数调用 js func,而不是该选项卡以相同的参数调用服务器代码。
比在代码中,您根据单击的选项卡加载所需的控件。
现在您需要将控件渲染为 html 并将其发送回 js 函数。
现在你已经有了js函数中的控件,找到你想要插入代码的位置并插入它。
理论上应该可行,而且并不复杂:))
I think the best solution is to implement client call back
http://msdn.microsoft.com/en-us/library/ms178210.aspx
When user clicks on some tab,onclick event calls js func with name of tab as parameter, than that tab calls server code with same parameter.
Than in code you load controls you want depending which tab is clicked.
Now you need to render controls into html and send tham back to js function.
Now you have controls in js function, find where you want to insert code an insert it.
that should work in theory and it is not to complicated :))
这个问题的答案(不是我的)可能对您有用:
异步在页面中加载用户控件
它指出,需要用户控件上的表单来回发存在问题,但是使用 ajax post 拥有独立的表单应该没问题。您必须考虑在页面上发布表单时会发生什么,但这不应该是不可克服的。不应该成为您不能将其设置为您提到的 ashx 处理程序的任何原因。
an asnwer (not mine) to this question is probably useful to you:
Asynchronous loading of user controls in a page
it states that there are problems with this with needing a form on the user control to post back, but that should be ok to have independent forms with ajax post. you'll have to think about what happens when posting the form(s) on the page, but shouldn't be insurmountable. shouldn't be any reason you couldn't just make it the ashx handler you have mentioned.