在Response.Redirect上保留ViewState [英] Retaining ViewState on a Response.Redirect

查看:67
本文介绍了在Response.Redirect上保留ViewState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好;



我有点困境,我不知道如何解决它。



我有一张表格,上面有一个下拉列表,其值会动态改变表格。还有一个提交按钮。点击提交按钮后,我需要刷新页面以显示在网格视图中对表单所做的更改。但我也需要下拉菜单来保留它的价值。目前,在button_click处理程序中,我让页面执行response.redirect。根据我的理解,问题是这不会保留使用下拉列表的视图状态。我尝试了一次server.transfer,一次是自己的,另一次使用preserveForm属性设置为true(这导致了堆栈溢出)。我在这一点上有点卡住了,非常感谢任何帮助。谢谢!

Hi Everyone;

I''ve got a bit of a dilemma, and I''m not sure how to solve it.

I''ve got a form, and on it, there''s a dropdownlist whose value changes the form dynamically. There''s also a submit button. When the submit button is hit, I need the page to refresh in order to show the changes that were made on the form in a gridview. But I also need the dropdown to retain it''s value. At current, in the button_click handler, I have the page doing a response.redirect. The problem, as I understand it, is this does not retain the viewstate for the dropdown to use. I tried a server.transfer, once on it''s own, and another time using the preserveForm property set to true (this caused a stack overflow). I''m a little stuck at this point and would really appreciate any help. Thanks!

推荐答案

你可以在这里做一些事情。



1.在下拉列表中你可以将auto post back属性设置为true并为其分配事件处理程序。这将导致每次选定值更改时表单都会刷新。然后有一个SelectedIndexChanged的事件处理程序,并在事件处理程序中相应地更新你的页面。

这是标记和C#代码隐藏

You can do a few things here.

1. On the dropdown list you can set the auto post back property to true and assign an event handler to it. This will cause the form to refresh every time a selected value changes. Then have an event handler for SelectedIndexChanged and inside the event handler update your page accordingly.
Here is the mark up and C# codebehind
<asp:dropdownlist runat="server" id="foo" autopostback="true" onselectedindexchanged="foo_SelectedIndexChanged" xmlns:asp="#unknown"></asp:dropdownlist>




protected void foo_SelectedIndexChanged(Object sender, EventArgs e){
}



2.如果您绝对必须使用Response.Redirect重定向,那么您可以在响应中添加一个参数,例如MyStuff / Languages / Alphabets.aspx?dropdownvalue = myvalue,当表单再次加载时,您可以读取此页面的值.Request.QueryString [ dropdownvalue]。一旦你读了这个值,只需设置下拉值。



触发页面重新加载


2. If you absolutely must redirect using Response.Redirect then you can add a parameter to your response like this MyStuff/Languages/Alphabets.aspx?dropdownvalue=myvalue and when the form loads again you can read the value like this Page.Request.QueryString["dropdownvalue"]. Once you read the value simply set the value of the drop down.

Trigger page reload

Response.Redirect("~/MyStuff/Languages/Alphabets.aspx?dropdownvalue=myvalue");

处理重新加载响应

Handle reload response

protected void Page_Load(object sender, EventArgs e)
{    
    string dropdownvalue = Page.Request.QueryString["dropdownvalue"];  
}


这篇关于在Response.Redirect上保留ViewState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆