asp.net Multiple Page_Load events for a user contr

2019-04-15 05:40发布

I've recently set up an ASP.net site (not using MVC.net) to use URL Routing (more on the code below) - when using user controls on the site (i.e I've created a "menu" user control to hold menu information) the page_load event for that control will fire twice when URLs have more than one variable passed over.

i.e.

pageName/VAR1 : will only fire the page_load event once.

while

pageName/VAR1/VAR2 : will fire the page_load event twice.

*Multiple extra VARs added on the end will still only fire the page_load event twice*.


Below are the code snippits from the files, the first is the MapPageRoute, located in the Global.asax :

// Register a route for the Example page, with the NodeID and also the Test123 variables allowed.
// This demonstrates how to have several items linked with the page routes.
    routes.MapPageRoute(
        "Multiple Data Example",                    // Route name
        "Example/{NodeID}/{test123}/{variable}",     // Route URL - note the NodeID bit
        "~/Example.aspx",                            // Web page to handle route
        true,                                        // Check for physical access
        new System.Web.Routing.RouteValueDictionary 
        { 
            { "NodeID", "1" },        // Default Node ID
            { "test123", "1" },       // Default addtional variable value
            { "variable", "hello"}    // Default test variable value
        }  
    );

Next is the way I've directed to the page in the menu item, this is a list item within a UL tag :

<li class="TopMenu_ListItem"><a href="<%= Page.GetRouteUrl("Multiple Data Example", new System.Web.Routing.RouteValueDictionary { { "NodeID", "4855" }, { "test123", "2" } }) %>">Example 2</a></li>

And finally the control that gets hit multiple times on a page load :

// For use when the page loads.
    protected void Page_Load(object sender, EventArgs e)
    {
        // Handle the routing variables.
        // this handles the route data value for NodeID - if the page was reached using URL Routing.
        if (Page.RouteData.Values["NodeID"] != null)
        {
            nodeID = Page.RouteData.Values["NodeID"] as string;
        };

        // this handles the route data value for Test123 - if the page was reached using URL Routing.
        if (Page.RouteData.Values["Test123"] != null)
        {
            ExampleOutput2.Text = "I am the output of the third variable : " + Page.RouteData.Values["Test123"] as string;
        };

        // this handles the route data value for variable - if the page was reached using URL Routing.
        if (Page.RouteData.Values["variable"] != null)
        {
            ExampleOutput3.Text = "I say " + Page.RouteData.Values["variable"] as string;
        };
    }

Note, that when I'm just hitting the page and it uses the default values for items, the reloads do not happen.


Any help or guidance that anyone can offer would be very much appreciated!

EDIT : The User Control is only added to the page once. I've tested the load sequence by putting a breakpoint in the page_load event - it only hits twice when the extra routes are added.

EDIT2 : Thanks again to those who've helped out so far - I'm still having trouble finding the cause of the double load - does anyone else have any more suggestions?

EDIT3/Answer : The answer below explains how to fix the problem - essentially remove any ../ references to Javascript files from the initial site creation/includes. I hope that this helps anyone else who has this problem!

Thanks in Advance,

Paul Hutson

6条回答
Anthone
2楼-- · 2019-04-15 06:08

It's not clear from your question whether or not you have multiple instances of this user control on the page. If so, I believe (though someone may correct me) that the control's page_load event will be fired for each copy.

A way to investigate this issue further is to put a breakpoint in the control's on_load event, and check the call stack each time it stops. It won't give you a definitive cause but, by checking the instance name & ID, etc you may be able to glean some more information.

查看更多
We Are One
3楼-- · 2019-04-15 06:11

I have gone around and around with this problem to the point of almost screaming. After commenting all code out i nailed the issue down to an iFrame with the an src attribute set with a default of '#'

I hope this helps someone.

查看更多
三岁会撩人
4楼-- · 2019-04-15 06:18

I dont know why but if you use visual to create asp project will have this code

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <script src="<%: ResolveUrl("~/Scripts/modernizr-2.6.2.js") %>"></script>
</asp:PlaceHolder>

If you remove the row

<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />

event Page_load will load only 1 time. :D it work for me

查看更多
老娘就宠你
5楼-- · 2019-04-15 06:27

Simply making sure that no file was referenced using ../ solved the problem entirely - specifically it was the Image files that were causing the problem. ex.- ../Image/xyz.png

When I have removed ../ then page is loading only once

查看更多
叼着烟拽天下
6楼-- · 2019-04-15 06:28

Its possibly you are trying to pass some querystrinvalues to the user control.Ca n you try passing them normally like below.

test.aspx?var1=sample&var2=test

查看更多
Rolldiameter
7楼-- · 2019-04-15 06:29

Having tried a number of things I have discovered that if any file has a ../ before it (in an include on any page) it will cause the URL Routing problem described above.

Simply making sure that no file was referenced using ../ solved the problem entirely - specifically it was the Javascript files that were causing the problem.

查看更多
登录 后发表回答