Response.Redirect(Request.Url.AbsoluteUri)和MultiView1.SetActiveView

时间:2014-03-08 15:31:24

标签: c# asp.net multiview

我有一份提交表格。当用户单击“保存”按钮时,应将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);
        }

}

1 个答案:

答案 0 :(得分:1)

以下代码将始终将视图设置为View1。

  if (!Page.IsPostBack) 
  {
      MultiView1.SetActiveView(View1);
  }

如果要在重定向后将ActiveView设置为特定视图,则可以在某处设置视图信息。例如SessionQueryString

查询字符串代码如下:

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);
        }
}