从ashx的使用ASP.Net服务器控件code代 [英] Use ASP.Net server control code generation from .ashx
问题描述
我想采取code的现有一堆是previously一个完整的.aspx页面上,并做同样的东西在ashx的处理程序。
在code创建的对象HTMLTABLE,添加行和单元格的那些行,然后补充说,HTML表格在.aspx的控件集合,然后把它添加到一个div,这是已经在页面上。
我试图保持code机智但不是把控制到一个div,实际生成的HTML,我会返回文本的一大块,可以通过AJAX客户端调用
HTMLTABLE错误,当我尝试使用InnerHtml属性(说它不支持),当我尝试RenderControl,使得第一个的TextWriter和明年的HtmlTextWriter对象后,我得到的页面不能为空的错误。
有没有人这样做呢?有什么建议?
* 最近是之上。
OK,甚至马特的更新之后,有一个解决办法;)
首先,我们必须使用一个网页格式
里面。否则,我们将无法添加的ScriptManager
控制。还有一件事:的ScriptManager
应控制在表单中的第一个控件。而且更容易:
页页=新页();
Button按钮=新System.Web.UI.WebControls.Button
{
ID =btnSumbit,
文本=TEXT按钮
UseSubmitBehavior =真
};
的HtmlForm形式=新的HtmlForm
{
ID =theForm
};
的ScriptManager ScriptManager的=新的ScriptManager
{
ID =ajaxScriptManager
};
form.Controls.Add(ScriptManager的);
form.Controls.Add(按钮);
page.Controls.Add(表);使用(StringWriter的输出=新的StringWriter())
{
HttpContext.Current.Server.Execute(页,输出,FALSE); context.Response.ContentType =text / plain的;
context.Response.Write(output.ToString());
}
这工作。输出是相当大的,所以我决定不把它列入到我的回答:)
其实,有一种变通方法。是的,我们可能会呈现处理程序的控制。
首先,我们需要一个无形的页面。因为没有它,我们得到:
类型控制btnSumbit'按钮'
必须放在一个表单标签里面加上
RUNAT =服务器。
块引用>公共类FormlessPage:页
{
公共覆盖无效VerifyRenderingInServerForm(控制控制)
{
}
}其次,没有人可以从创建我们的
FormlessPage
页的一个实例prevent我们。现在,让我们添加一个控制那里(我决定增加一个按钮
控制作为一个例子,但是你可以使用任何)。FormlessPage页=新FormlessPage();
Button按钮=新System.Web.UI.WebControls.Button
{
ID =btnSumbit,
文本=TEXT按钮
UseSubmitBehavior =真
};
page.Controls.Add(按钮);第三,我们捕获输出。为此,我们使用
HttpServerUtility.Execute
方法:
进行指定的处理程序
在的情况下虚拟路径
当前的请求。一个
System.IO.TextWriter
捕捉输出
从执行的处理程序和
布尔参数指定是否
清除
System.Web.Htt prequest.QueryString
和
System.Web.Htt prequest.Form
集合。
块引用>下面是code:
使用(StringWriter的输出=新的StringWriter())
{
HttpContext.Current.Server.Execute(页,输出,FALSE); context.Response.ContentType =text / plain的;
context.Response.Write(output.ToString());
}其结果将是:
<输入类型=提交名称=btnSumbitVALUE =TEXT按钮ID =btnSumbit/>
另外,我可以推荐ScottGu的文章<一个href=\"http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx\"相对=nofollow>技巧/诀窍:酷UI模板技术与ASP.NET AJAX使用非UpdatePanel的场景的。希望你能找到很多有用的存在。
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?
解决方案*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: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 aButton
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 theSystem.Web.HttpRequest.QueryString
andSystem.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.
这篇关于从ashx的使用ASP.Net服务器控件code代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!