【发布时间】:2010-11-06 06:47:43
【问题描述】:
我希望处理程序重定向到 Web 表单页面,预先填写表单上某些控件的值。
我尝试设置我当前的 Request.Form 数据:
if (theyWantToDoSomething)
{
//pre-fill form values
context.Request.Form["TextBox1"] = "test";
context.Request.Form["ComboBox1"] = "test 2";
context.Request.Form["TextBox2"] = GetTheTextForTheThing();
//tell the client to go there
context.Response.Redirect("~/SomeWebForm.aspx");
return;
}
但我得到一个例外,即 Form 值是只读的。
有什么方法可以将客户端发送到另一个页面,预先填写表单数据?
回答
我使用会话状态来存储值。请务必注意,默认情况下 Handler 无权访问 Session(Session 对象将为 null)。您通过将IRequiresSessionState 标记接口添加到您的处理程序类来have to tell IIS to give you the Session 对象:
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
...
if (theyWantToDoSomething)
{
//pre-fill form values
context.Session["thing1"] = "test";
context.Session["thing2"] = "test 2";
context.Session["thing3"] = GetTheTextForTheThing();
//tell the client to go there
context.Response.Redirect("~/SomeWebForm.aspx");
return; //not strictly needed, since Redirect ends processing
}
...
}
}
【问题讨论】:
标签: asp.net forms post redirect response.redirect