Вопрос

I have a submit form. when user click save button it should set active view of multiview1 to view2. I added Response.Redirect(Request.Url.AbsoluteUri); to prohibit users from pressing F5 button and submitting the form again and again, but it causes to multiview1 not set active view to view2 and after submiting the form still shows 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);

    }
}

and this is my pageload event:

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

}
Это было полезно?

Решение

The below code will always set the view to View1.

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

If you want to set the ActiveView to a specific view after the redirect then you have set your view information somewhere. like Session or QueryString

Query string code will be like:

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");// 

     }
}

And on 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);
        }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top