Ian*_*oyd 2 forms asp.net post redirect response.redirect
我想要一个处理程序重定向到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;
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外,Form值是只读的.
将客户端发送到另一个页面的方法是什么,预先填写表单数据?
我使用Session状态来存储值.重要的是要注意,默认情况下,Handler无权访问Session(Session对象将为null).您必须通过将IRequiresSessionState标记接口添加到处理程序类来告诉IIS为您提供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
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
您只能填充响应,请求是输入数据,并且确实是只读的.
如果您使用的是ASP.NET,可以通过多种方式完成所需的操作:
最好的方法可能是通过Session对象将需要预先填充的数据传递给SomeWebForm.aspx,并在那些页面上使用Load方法填充表单.请记住,当您执行Response.Redirect时,会使用客户端应重定向到的URL向客户端发送302响应.这个过程对用户来说是透明的......但是需要完整的往返行程.
填充用户Session的另一种方法是通过查询字符串将GET参数添加到SomeWebForm.aspx的重定向.
如果您需要将处理转移到SomeWebForm.aspx页面而不进行循环跳转,则可以使用Server.Transfer.这会将执行从当前页面转移到您选择的页面...但是,这可能会导致客户端出现一些奇怪的行为,因为URL不会更新.就用户而言,它仍然看起来好像是在他们开始的同一页面上.