使用 jQuery 将用户控件的输出返回为 HTML 或字符串

发布于 2024-12-19 18:24:19 字数 1155 浏览 4 评论 0原文

今天早上,我试图弄清楚如何使用 jQuery 从页面上的按钮更新用户控件。

我发布了这个问题

使用 jQuery 刷新用户控件

从本文中收集的信息:

< a href="http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery" rel="nofollow noreferrer">http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery

在发布该问题后我收到了出色的帮助,我现在有了使用 jQuery 将用户控件加载为页面的方法,使用 codeproject.com 文章中转换后的 .VB 示例。

但是...这不会渲染到浏览器中,Firebug 给出错误“元素必须包含在表单 runat=server 标记中”

所以我发布了这个问题:

https://stackoverflow。 com/questions/8388627/re-better-way-to-manage-a-dynamic-select-list-using-jquery-and-user-control

自从发布后,我进一步考虑了它,并且相信事实上,由 codeproject 代码创建的 jQueryHandler 类向浏览器发送一个完整的页面。

遗憾的是,鉴于我并不真正理解该 codeproject 代码的实际用途,我无法弄清楚如何更改它。

我需要它做的根本不是返回一个 Page,而是返回一个 HTML 字符串,然后可以根据需要将其作为 HTML 直接粘贴到 div 中。

关于如何更改该代码以返回 HTML 字符串有什么想法吗?

(我使用的是asp.net 2.0和VB)

This morning I was trying to work out how to update a User Control from a button on a page using jQuery.

I posted this question

Refresh User Control with jQuery

From the info gleaned from this article:

http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery

And the excellent help I received following posting that question, I now have a method of loading a User Control as a page with jQuery using the converted .VB example from the codeproject.com article.

However... this won't render into the browser, Firebug is giving the error "element must be contained within a form runat=server tag"

So I posted this question:

https://stackoverflow.com/questions/8388627/re-better-way-to-manage-a-dynamic-select-list-using-jquery-and-user-control

Since posting that though, I have thought about it further, and believe it is the fact that the jQueryHandler class created by the codeproject code is send the browser a complete page.

Given that I sadly don't really understand what that codeproject code actually does I am having trouble working out how to alter it.

What I need it to do is not return a Page at all, but I guess, an HTML string, which can then be pasted directly as HTML into the div as required.

Any ideas as to how to alter that code to return an HTML string?

(I am using asp.net 2.0 and VB)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2024-12-26 18:24:19

回发将导致整个页面刷新。既然您想避免这种情况,那么我建议使用 jquery 并动态创建选项 html 并将其绑定到

用户控件中的单击事件,只需键入对 js 文件的引用。

<script type="text/javascript" src="/userControl.js"></script>

//set the page elements that you want rendered that will exists in every location that the user control is rendered.

<div>
  //Set your layout
  .
  .
  .
  //set the container for the dynamically chaning content
  <div id="dynamicContent"></div>
  <input type="button" id="ClickMe" />
</div>

在脚本文件中你可以做这样的事情

$(document).Ready(function() {
  $("#ClickMe").click(GenerateContent)
});

function GenerateContent()
{
  var dynamicHTML;
  //condition == whatever conditions you are trying to validate to change the content
  if(condition)
  {
    dynamicHTML = ""; //set page specific content
  }
  $(dynamicHTML).appendTo("#dynamicContent");
}

The postback would cause a full page refresh. Since you are wanting to avoid that then what I would recommend is this use jquery and dynamically create the options html and bind that to a click event

in the user control just type a reference to a js file.

<script type="text/javascript" src="/userControl.js"></script>

//set the page elements that you want rendered that will exists in every location that the user control is rendered.

<div>
  //Set your layout
  .
  .
  .
  //set the container for the dynamically chaning content
  <div id="dynamicContent"></div>
  <input type="button" id="ClickMe" />
</div>

In the script file you can then do something like this

$(document).Ready(function() {
  $("#ClickMe").click(GenerateContent)
});

function GenerateContent()
{
  var dynamicHTML;
  //condition == whatever conditions you are trying to validate to change the content
  if(condition)
  {
    dynamicHTML = ""; //set page specific content
  }
  $(dynamicHTML).appendTo("#dynamicContent");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文