Alok Sharma
Alok Sharma

Reputation: 115

Master Page is getting called twice in Response.Redirect asp.net c#

When I change the index in masterpage dropdown list. This is what happens:

1. The current page load event is getting called .

2. Master Page contents gets called and it does not hit (!PostBack) functions which is how it is supposed to be .

3. Selected index changed event is called from where it is redirected to destination Page

4. Destination Page content is getting called.

5. Master Page content is getting called as and this time it hits (!PostBack) functions.due to that my selected value gets reset as I take this value from database and insert first time .

I am confused as to why it is happening, I tried many things but didn't get the result. Also I am not sure whether step 1 and step 2 are also correct process.

I am really struggling with this and need some help.

This is my code

SourcePage.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PlantOverview.aspx.cs" Inherits="PlantDevelopment.PlantOverview" MasterPageFile="~/Master/MasterPage2.master" %>


    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </asp:Content>

    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <div class="flex-lg-wrap wrapper">
        </div>
    </asp:Content>

SourcePage.aspx.cs

    using System;

    namespace PlantDevelopment
    {
        public partial class PlantOverview : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

                if (!IsPostBack)
                {
                }

            }
        }
    }

DestinationPage.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/MasterPage2.master" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="PlantDevelopment.TestPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
   <!-- Latest compiled JavaScript -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <style type="text/css">
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div class="flex-lg-wrap wrapper">
    </div>
</asp:Content>   

Destinationpage.aspx.cs

    using System;

    namespace PlantDevelopment
    {
        public partial class TestPage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {



                }

            }
        }
    }

MasterPage.Master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage2.master.cs" Inherits="Master_MasterPage2" %>

    <!DOCTYPE html>

    <html>
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
            <nav class="navbar navbar-expand-lg" style="background-color: #081A51; margin-top: 15px;">
                <div>
                    <div class=" justify-content-end">
                        <div class="btn-group btn-style1">
                            <asp:DropDownList ID="DD_Line" class="dropDownWindow_master" runat="server" Width="350px" ForeColor="black" Font-Size="22px" AutoPostBack="true" OnSelectedIndexChanged="DD_Line_SelectedIndexChanged">
                                <asp:ListItem Selected="True" Value="0" Text="Select Line"></asp:ListItem>
                                <asp:ListItem Value="Line1"></asp:ListItem>
                                <asp:ListItem Value="Line2"></asp:ListItem>
                                <asp:ListItem Value="Line3"></asp:ListItem>
                            </asp:DropDownList>
                        </div>

                    </div>
                </div>
            </nav>
            <div class="content" id="pageContent">
                <asp:ContentPlaceHolder runat="server" ID="ContentPlaceHolder1"></asp:ContentPlaceHolder>
                <div class="footer-wrapper" id="footerWrapper">
                </div>
            </div>
        </form>
    </body>
    </html>

MasterPage.cs

    using System;
    using System.Web.UI;

    public partial class Master_MasterPage2 : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //blanks dropdown and insert from DB (this is the issue the previous selected value gets reset)
            }
        }

        protected void DD_Line_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TestPage.aspx", false);
        }
    }

Upvotes: 1

Views: 426

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49169

Ok, we might have to step back a bit more.

When using a master page, and you do a simple navigate to "some" page That page of course is a URL based on the "child" page.

So, when above occurs, then we find:

child page load event fires - (postback = false)
master page load event fire (postback = false).

Ok, at this point in time, then any button click (child page).

then

Child page load event tigger, post-back flag = True
master page load event trigger, post-back flag = True
our button click (or event) in child page runs.
(or event stub in master page runs if event from masterpage)

So, in ALL cases, both child page load, master page load will fire.

However, if we navigate to a new page?

Then we are in effect starting over, and the master page is to be considered a full re-load, and post-back will be false on first page load (after navigate) to the new page. (this holds true for both master + child page).

So, master page does not "only" fire one time on the whole application start, and thus only ever have ONE postback event = false, and then additional post-backs (from master or child) will then of course have post-back = true.

And in fact, page load for master page triggers every time for any event on the current page (just like child page load triggers every time also).

Of course, if we in the child (or master) we re-navigate to the same page using code?

Then again we are starting over.

If you have a button (say in child page) that does some stuff, and THEN navigates to the SAME page, then yes, the master page load event will fire 2 times. (so will the child page load event).

So, a trigger in code such as a response.Redirect() to the same page should be avoided, since it will trigger both load events two times. So, the reason being is that in the current page, if we trigger a button click/event, then both page loads will trigger as always, then the child event code stub runs. But, if for some reason that trigger is a navigate to the SAME page?

Then again we starting over, and both load events will trigger again, - and both master/child will show is-postback = false.

So, a "navigate" to the same page will trigger both on-loads, and both will be postback = false. And this holds true EVEN if we are/were on the SAME current page when that navigation occurs.

And for such navigation to occur on the same page means both load events will have triggered, then your code stub run, and then assuming that stub triggers navigation to the same page, then that will of course cause both post-backs to occur again. (with post-back = false, like we are starting over again). So yes, this is expected, and thus we get 2 page loads for master and child.

From what you suggest/state, the solution would be to remove the current page code that is causing a navigation to the same page. (maybe you test/check the current url then???).

So for any event (in child or master page), then both events trigger (PostBacks = true). However, if you navigate to the same page (as a result of running that event)?

Then if that navigation is the result of code behind in the same page? Well, then yes, we will see/have both load events trigger two times, first time is the "button" or code that we run from that page. (which means postback = true). The 2 load events fire, then we navigate, and if we navigate to the same page, then we are in effect starting over. In such a case, 4 load events will have occurred.

As I stated, the solution is to NOT use code to navigate to the same page we are on, and I see "little" reason for this to occur. About the only time I had code navigate to same page in code behind was some code to re-fresh/re-load the current URL to re-set the page).

Upvotes: 2

Related Questions