使用 .ashx 生成 ASP.Net 服务器控制代码
我正在尝试获取以前位于完整 .aspx 页面上的一组现有代码,并在 .ashx 处理程序中执行相同的操作。
该代码创建了一个 HtmlTable 对象,向这些行添加了行和单元格,然后将该 html 表添加到 .aspx 的控件集合中,然后将其添加到页面上已有的 div 中。
我试图保持代码完整,但不是将控件放入 div 中,而是实际生成 html,然后我将在可通过 AJAX 客户端调用的一大块文本中返回它。
当我尝试使用 InnerHtml 属性(说不支持)时,HtmlTable 出错,当我尝试 RenderControl 时,在先创建一个 TextWriter,然后创建一个 HtmlTextWriter 对象后,出现 Page 不能为 null 的错误。
以前有人这样做过吗?有什么建议吗?
I'm trying to take an existing bunch of code that was previously on a full .aspx page, and do the same stuff in a .ashx handler.
The code created an HtmlTable object, added rows and cells to those rows, then added that html table the .aspx's controls collection, then added it to a div that was already on the page.
I am trying to keep the code in tact but instead of putting the control into a div, actually generate the html and I'll return that in a big chunk of text that can be called via AJAX client-side.
HtmlTable errors out when I try to use the InnerHtml property (says it isn't supported), and when I try RenderControl, after making first a TextWriter and next an HtmlTextWriter object, I get the error that Page cannot be null.
Has anyone done this before? Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*最近的内容在上面。
好的,即使在 Matt 更新之后,仍然有一个解决方法;)
首先,我们必须使用一个内部带有
form
的页面。否则我们将无法添加ScriptManager
控件。还有一件事:ScriptManager
控件应该是表单中的第一个控件。进一步更容易:这有效。输出非常大,所以我决定不将其包含在我的答案中:)
实际上,有一个解决方法。是的,我们可以在处理程序中渲染一个控件。
首先,我们需要一个无形式的页面。因为没有它我们会得到:
其次,没有人可以阻止我们创建
FormlessPage
页面的实例。现在让我们在那里添加一个控件(我决定添加一个Button
控件作为示例,但您可以使用任何控件)。第三,让我们捕获输出。为此,我们使用 HttpServerUtility.Execute 方法:
代码如下:
结果将是:
另外我可以推荐 ScottGu 的文章提示/技巧:与 ASP.NET AJAX 一起用于非 UpdatePanel 方案的超酷 UI 模板技术。希望您能在那里找到很多有用的东西。
*Most recent is above.
OK, even after Matt's update there is a workaround ;)
Firstly, we have to use a page with
form
inside. Otherwise we won't be able to add aScriptManager
control. One more thing: theScriptManager
control should be the first control in the form. Further is easier:This works. The output is quite large so I decided not to include it into my answer :)
Actually, there is a workaround. Yep, we may render a control in handler.
Firstly, we need a formless page. Because without it we get:
Secondly, nobody can prevent us from creating an instance of our
FormlessPage
page. And now let's add a control there (I decided to add aButton
control as an example, but you could use any).Thirdly, let's capture the output. For this we use
HttpServerUtility.Execute
method:Here is the code:
The result will be:
<input type="submit" name="btnSumbit" value="TextButton" id="btnSumbit" />
In addition I can recommend ScottGu's article Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios. Hope, you could find a lot of useful there.
另一种选择是在进程中托管 ASP.NET HTTP 管道,将页面呈现到流,并在处理页面后读取需要从 HttpListenerContext.Response.OutputStream 流发送的 HTML。
本文有详细信息:http://msdn.microsoft.com/en-us /杂志/cc163879.aspx
Another option is to host the ASP.NET HTTP pipeline in your process, render the page to a stream and read the HTML you need to send from the HttpListenerContext.Response.OutputStream stream after the page has been processed.
This article has details: http://msdn.microsoft.com/en-us/magazine/cc163879.aspx