*最新のものは上記です。
OK、マットの更新後でも回避策があります;)
form
まず、内部のあるページを使用する必要があります。ScriptManager
そうしないと、コントロールを追加できません。もう1つ、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());
}
これは機能します。出力はかなり大きいので、答えに含めないことにしました:)
実際には、回避策があります。はい、ハンドラーでコントロールをレンダリングする場合があります。
まず、フォームのないページが必要です。それがないと、次のようになります。
タイプ「ボタン」のコントロール「btnSumbit」は、runat=serverのフォームタグ内に配置する必要があります。
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());
}
結果は次のようになります。
<input type="submit" name="btnSumbit" value="TextButton" id="btnSumbit" />
さらに、ScottGuの記事「ヒント/トリック:UpdatePanel以外のシナリオでASP.NETAJAXで使用するクールなUIテンプレート手法」をお勧めします。そこにたくさんの便利なものが見つかることを願っています。