我有一份提交表格。当用户单击“保存”按钮时,应将multiview1的活动视图设置为view2。我添加了Response.Redirect(Request.Url.AbsoluteUri);禁止用户按F5键并反复提交表单,但是导致multiview1没有将活动视图设置为view2并且在提交表单后仍显示view1
protected void btnSubmitAd_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Ads ad = new Ads
{
Title = txtAdTitle.Text,
Dec = txtAdText.Text,
Name = txtName.Text,
Email = txtEmail.Text
};
context.Ads.Add(ad);
context.SaveChanges();
MultiView1.SetActiveView(View2);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
这是我的页面加载事件:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
MultiView1.SetActiveView(View1);
}
}
答案 0 :(得分:1)
以下代码将始终将视图设置为View1。
if (!Page.IsPostBack)
{
MultiView1.SetActiveView(View1);
}
如果要在重定向后将ActiveView
设置为特定视图,则可以在某处设置视图信息。例如Session
或QueryString
查询字符串代码如下:
protected void btnSubmitAd_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Ads ad = new Ads
{
Title = txtAdTitle.Text,
Dec = txtAdText.Text,
Name = txtName.Text,
Email = txtEmail.Text
};
context.Ads.Add(ad);
context.SaveChanges();
//MultiView1.SetActiveView(View2); No need for that as it will be lost after redirect...
//Append your ActiveView information in query string with Request.Url.AbsoluteUri
Response.Redirect(Request.Url.AbsoluteUri + "?activeView=View2");//
}
}
并PageLoad
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string activeView = Request.QueryString["activeView"]
if(!string.IsNullOrEmpty(activeView) && activeView == "View2")
MultiView1.SetActiveView(View2);
else
MultiView1.SetActiveView(View1);
}
}