简体   繁体   English

asp.net使用URL路由时用户控件的多个Page_Load事件

[英]asp.net Multiple Page_Load events for a user control when using URL Routing

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 (ie 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. 我最近设置了一个ASP.net站点(不使用MVC.net)以使用URL路由(下面的代码更多)-在站点上使用用户控件时(即,我创建了一个“菜单”用户控件来保留菜单信息),当URL传递了多个变量时,该控件的page_load事件将触发两次。

ie

pageName/VAR1 : will only fire the page_load event once. pageName / VAR1:只会触发一次page_load事件。

while

pageName/VAR1/VAR2 : will fire the page_load event twice. pageName / VAR1 / VAR2:将触发两次page_load事件。

*Multiple extra VARs added on the end will still only fire the page_load event twice*. *最后添加的多个额外VAR仍只会触发page_load事件两次*。


Below are the code snippits from the files, the first is the MapPageRoute, located in the Global.asax : 以下是文件中的代码片段,第一个是位于Global.asax中的MapPageRoute:

// 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 : 接下来是我定向到菜单项中的页面的方式,这是UL标签内的列表项:

<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. 我已经通过在page_load事件中放置一个断点来测试加载顺序-当添加额外的路由时,它只会命中两次。

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? EDIT2:再次感谢那些到目前为止提供帮助的人-我仍然很难找到造成双重负担的原因-其他人还有其他建议吗?

EDIT3/Answer : The answer below explains how to fix the problem - essentially remove any ../ references to Javascript files from the initial site creation/includes. EDIT3 /答案:以下答案说明了如何解决此问题-本质上是从初始站点创建/包含中删除对Javascript文件的任何../引用。 I hope that this helps anyone else who has this problem! 我希望这对有此问题的其他人有所帮助!

Thanks in Advance, 提前致谢,

Paul Hutson 保罗·赫特森

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. 尝试了许多操作后,我发现,如果任何文件之前(在任何页面上的包含文件中)都带有../,将导致上述URL路由问题。

Simply making sure that no file was referenced using ../ solved the problem entirely - specifically it was the Javascript files that were causing the problem. 只需确保没有使用../引用任何文件就可以完全解决问题-特别是导致问题的Javascript文件。

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 '#' 在注释掉所有代码之后,我将问题固定到了一个iFrame,该iFrame的src属性设置为默认值“#”

I hope this helps someone. 我希望这可以帮助别人。

I dont know why but if you use visual to create asp project will have this code 我不知道为什么,但是如果您使用Visual来创建ASP项目,则会有以下代码

<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. 事件Page_load将仅加载1次。 :D it work for me :D它为我工作

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. 如果是这样,我相信 (尽管有人会纠正我),将为每个副本触发该控件的page_load事件。

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. 进一步研究此问题的一种方法是在控件的on_load事件中放置一个断点,并在每次停止时检查调用堆栈。 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. 它不会给您确定的原因,但是通过检查实例名称和ID等,您也许可以收集到更多信息。

Its possibly you are trying to pass some querystrinvalues to the user control.Ca n you try passing them normally like below. 它可能是您试图将一些querystrinvalues传递给用户控件。您可以像下面这样正常地传递它们。

test.aspx?var1=sample&var2=test Test.aspx文件?VAR1 =样品VAR2 =测试

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 例如-../Image/xyz.png

When I have removed ../ then page is loading only once 当我删除../之后,页面仅加载一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM