通常,您可以通过制作一个aspx web表单来实现这一点,该表单使用querystring来通知要加载/处理的记录.
假设您有一个页面,可以更新一些客户信息:
http://www.mysite.com/customer.aspx
您可以使用querystring中的id加载表单:
http://www.mysite.com/customer.aspx?CustomerId=42
在代码后面,您会看到如下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int customerId = 0;
if (!string.IsNullOrEmpty(Request.QueryString["CustomerId"]))
{
int.TryParse(Request.QueryString["CustomerId"], out customerId );
}
if (customerId == 0)
{
//handle case when no valid customer id was passed in the qs here
}
else
{
//load customer details, bind controls etc
//make sure to handle the case when no customer was found using the id in the qs
}
}
}
然后在页面的某个地方会有一个保存更改的按钮.该按钮的代码中会有一个OnClick处理程序:
protected void SaveClicked(object sender, EventArgs e)
{
//save changes to database here
//Redirect if all went well
Response.Redirect("http://www.mysite.com/customer.aspx?CustomerId="
+ idOfSavedCustomer.ToString());
}
基本上就是这样.重定向将导致浏览器对重定向中的url发出新的GET请求(…).它将加载页面,if (!IsPostBack)
将运行,并使用您在上一篇文章中刚刚保存的新值初始化页面.
在整个过程中,浏览器和服务器之间的通信量如下所示:
Browser: GET http://www.mysite.com/customer.aspx?CustomerId=42
Server: 200 (send back some html)
Browser: POST http://www.mysite.com/customer.aspx?CustomerId=42 (post data sent in request)
Server: 302 (point to http://www.mysite.com/customer.aspx?CustomerId=42)
Browser: GET http://www.mysite.com/customer.aspx?CustomerId=42
Server: 200 (send html)
在中间步骤中,服务器基本上说:
"That post request you sent me, I'm done with that. Now please got to this other page here..."
url实际上指向同一页面这一事实并不重要.
针对你的要点问题 list ,你可以做一些思考:
可以通过在窗体上设置action
属性来实现,也可以在按钮上设置PostBackUrl
属性.
- 当您发布到未显示的表单时,视图状态会发生什么变化
是否读取视图状态?
Depends. If you simply post the form to a different page, you can use the <%@ PreviousPageType .../> directive to tell the "new" page where the post came from. This will simplyfy working with the posted data on the new page. See this link for details.
- 当您重定向到"真正的"ASPX时,视图状态会发生什么变化
网页表单?
视图状态在post请求中发送.重定向时,浏览器将加载一个新页面,并创建自己的状态.
这要看你怎么看了.重定向之后,新页面以前将无法访问该页面的视图状态.
- 是ASP.net从根本上与Post重定向不兼容--Get?
不是的.请参见上面的示例.
- 如何(即什么代码)重定向到"真正的"ASPX web表单?
回答重定向(url).这将向浏览器发送响应,告诉它执行新的get请求.
- 何时(即在哪个事件处理程序中)重定向到"真正的"ASPX
网页表单?
完成处理post请求所需的所有工作后.
重定向post请求没有得到很好的支持,可能应该避免.可以通过使用http响应307(使用某些浏览器)来完成.这样做时,服务器实际上会告诉浏览器"I will not process you post request, please post it to this other page instead".
服务器转移(…)是服务器端正在发生的事情.浏览器没有意识到这一点.基本上,一个页面可以使用服务器.传输以便让som其他页面进行一些处理,该页面将负责将响应发送回浏览器.但浏览器会认为这是响应的原始页面.
- aspx或aspx中必须发生的代码更改.要支持的cs文件
不,可以使用普通的回邮.诀窍是在页面上有一个(或几个)特定的事件处理程序进行重复.处理发布的数据后重定向.