使用 .ashx 生成 ASP.Net 服务器控制代码

发布于 2024-08-30 20:13:28 字数 389 浏览 10 评论 0原文

我正在尝试获取以前位于完整 .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 技术交流群。

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

发布评论

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

评论(2

好倦 2024-09-06 20:13:28

*最近的内容在上面。

好的,即使在 Matt 更新之后,仍然有一个解决方法;)

首先,我们必须使用一个内部带有 form 的页面。否则我们将无法添加 ScriptManager 控件。还有一件事:ScriptManager 控件应该是表单中的第一个控件。进一步更容易:

Page page = new Page();
Button button = new System.Web.UI.WebControls.Button
{
    ID = "btnSumbit",
    Text = "TextButton",
    UseSubmitBehavior = true
};
HtmlForm form = new HtmlForm
{
    ID="theForm"
};
ScriptManager scriptManager = new ScriptManager
{
    ID = "ajaxScriptManager"
};
form.Controls.Add(scriptManager);
form.Controls.Add(button);
page.Controls.Add(form);

using (StringWriter output = new StringWriter())
{
    HttpContext.Current.Server.Execute(page, output, false);

    context.Response.ContentType = "text/plain";
    context.Response.Write(output.ToString());
}

这有效。输出非常大,所以我决定不将其包含在我的答案中:)


实际上,有一个解决方法。是的,我们可以在处理程序中渲染一个控件。

首先,我们需要一个无形式的页面。因为没有它我们会得到:

控制“Button”类型的“btnSumbit”
必须放在表单标签内
runat=服务器。

public class FormlessPage : Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

其次,没有人可以阻止我们创建 FormlessPage 页面的实例。现在让我们在那里添加一个控件(我决定添加一个 Button 控件作为示例,但您可以使用任何控件)。

FormlessPage page = new FormlessPage();
Button button = new System.Web.UI.WebControls.Button
{
    ID = "btnSumbit",
    Text = "TextButton",
    UseSubmitBehavior = true
};
page.Controls.Add(button);

第三,让我们捕获输出。为此,我们使用 HttpServerUtility.Execute 方法:

执行指定的处理程序
上下文中的虚拟路径
当前请求。一个
System.IO.TextWriter 捕获输出
来自执行的处理程序和
布尔参数指定是否
清除
System.Web.HttpRequest.QueryString 和
System.Web.HttpRequest.Form
收藏。

代码如下:

using (StringWriter output = new StringWriter())
{
    HttpContext.Current.Server.Execute(page, output, false);

    context.Response.ContentType = "text/plain";
    context.Response.Write(output.ToString());
}

结果将是:

另外我可以推荐 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 a ScriptManager control. One more thing: the ScriptManager control should be the first control in the form. Further is easier:

Page page = new Page();
Button button = new System.Web.UI.WebControls.Button
{
    ID = "btnSumbit",
    Text = "TextButton",
    UseSubmitBehavior = true
};
HtmlForm form = new HtmlForm
{
    ID="theForm"
};
ScriptManager scriptManager = new ScriptManager
{
    ID = "ajaxScriptManager"
};
form.Controls.Add(scriptManager);
form.Controls.Add(button);
page.Controls.Add(form);

using (StringWriter output = new StringWriter())
{
    HttpContext.Current.Server.Execute(page, output, false);

    context.Response.ContentType = "text/plain";
    context.Response.Write(output.ToString());
}

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:

Control 'btnSumbit' of type 'Button'
must be placed inside a form tag with
runat=server.

public class FormlessPage : Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

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 a Button control as an example, but you could use any).

FormlessPage page = new FormlessPage();
Button button = new System.Web.UI.WebControls.Button
{
    ID = "btnSumbit",
    Text = "TextButton",
    UseSubmitBehavior = true
};
page.Controls.Add(button);

Thirdly, let's capture the output. For this we use HttpServerUtility.Execute method:

Executes the handler for the specified
virtual path in the context of the
current request. A
System.IO.TextWriter captures output
from the executed handler and a
Boolean parameter specifies whether to
clear the
System.Web.HttpRequest.QueryString and
System.Web.HttpRequest.Form
collections.

Here is the code:

using (StringWriter output = new StringWriter())
{
    HttpContext.Current.Server.Execute(page, output, false);

    context.Response.ContentType = "text/plain";
    context.Response.Write(output.ToString());
}

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.

茶花眉 2024-09-06 20:13:28

另一种选择是在进程中托管 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文