Response.Redirect(Request.Url.AbsoluteUri) and MultiView1.Se



我有一个提交表格。当用户单击"保存"按钮时,它应该将Multiview1的活动视图设置为View2。我添加了响应。为了禁止用户按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);
    }
}

这是我的pageload事件:

protected void Page_Load(object sender, EventArgs e)
{
        if (!Page.IsPostBack) {
            MultiView1.SetActiveView(View1);
        }
}

以下代码将始终将视图设置为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);
        }
}

最新更新