通过常规浏览器请求和 AJAX 请求呈现的视图用户控件中的 asp.net mvc JavaScript
我的一些 ASCX 文件中有此代码:
<%=Html.ActionLink(Resources.Localize.Routes_WidgetsEdit, "Edit", "Widget",
new { contentType = Model.ContentType, widgetSlug = Model.Slug, modal=true},
new
{
rel = "shadowbox;height=600;width=700",
title = Resources.Localize.Routes_WidgetsEdit,
@class = "editWidget"
})%>
请注意那里的 rel="shadowbox..." 。这是为此 ActionLink 连接 ShadowBox Lightbox 克隆。
当用户通过正常浏览器请求请求包含此用户控件的页面时,这种方法可以正常工作。但我还通过 AJAX 请求渲染/构建这些“查看用户”控件。例如,我将使用 jQuery .ajax() 方法向 /Widget/RenderToString/... 发出请求,它将返回该控件的 HTML 代码。这工作正常并且可以很好地呈现代码。然后,我会将结果插入(附加)到发出 AJAX 请求的页面中的 DIV 中。这也可以正常工作,并且返回的 HTML 会被附加。唯一的问题是 - ShadowBox 没有连接。即使它的代码被渲染。
似乎每次连接 ShadowBox 都需要重新加载页面(F5)。由于我正在执行 AJAX GET 和即时追加以避免服务器往返,因此我还希望 ShadowBox 无需刷新即可连接。
有人可以帮我吗?谢谢
更新:
是的,我的 Site.Master 头中有这个:
<script src="<%=Url.Content("~/Scripts/shadowbox-build-3.0rc1/shadowbox.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
// insert functions calls here that provide some default behaviour
externalLinks();
});
Shadowbox.init({
language: "en",
players: ["img", "html", "iframe"],
onClose: function() { location.reload(true) }
});
</script>
如何在 AJAX 调用后再次初始化 Shadowbox?
I have this code in some of my ASCX files:
<%=Html.ActionLink(Resources.Localize.Routes_WidgetsEdit, "Edit", "Widget",
new { contentType = Model.ContentType, widgetSlug = Model.Slug, modal=true},
new
{
rel = "shadowbox;height=600;width=700",
title = Resources.Localize.Routes_WidgetsEdit,
@class = "editWidget"
})%>
Take note of that rel="shadowbox..." there. This is to wire up ShadowBox Lightbox clone for this ActionLink.
This works fine when user requests a page containing this User Control thru normal browser request. But I also render/build those View User controls trough AJAX requests. For instance, I would make request to /Widget/RenderToString/... using jQuery .ajax() method and it would return HTML code for that control. This works fine and it renders the code fine. I would then insert (append) the result to a DIV in a page from where the AJAX request was made. This also works fine and the returned HTML gets appended. The only problem is - ShadowBox is not wired up. Even though the code for it gets rendered.
It seems it requires page reload (F5) every time to wire ShadowBox up. Since I am doing AJAX GET and instant append to get rid of having to make a server roundtrip, I would also want ShadowBox to wire up without doing refresh.
Can someone help me with that? Thank you
UPDATE:
Yes, I have this in my Site.Master head:
<script src="<%=Url.Content("~/Scripts/shadowbox-build-3.0rc1/shadowbox.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
// insert functions calls here that provide some default behaviour
externalLinks();
});
Shadowbox.init({
language: "en",
players: ["img", "html", "iframe"],
onClose: function() { location.reload(true) }
});
</script>
How do I init the Shadowbox again after AJAX call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Shadowbox 插件有很多...您使用哪一个? (如果没有它,我无法给你确切的代码。)无论如何,我想你的
$(document).ready(function () { ... });
中有一些东西可以告诉shadowbox插入以束缚自身。您需要在 AJAX 调用之后再次调用它。There are many shadowbox plugins... which one are you using? (I can't give you exact code without it.) In any case I imagine you have something in your
$(document).ready(function () { ... });
that tells shadowbox plungin to bind itself. You need to call that again after the AJAX call.刚刚在此处找到了解决方案
Just found the solution here