本文介绍了使用URL路由,当用户控件asp.net多的Page_Load事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我最近成立了一个ASP.net网站(不使用MVC.net)使用URL路由(在code详见下文) - 使用用户控件时,该网站上(即我已经创建了一个菜单中的用户控制持有菜单信息),该控件的URL时有多个变量传递过来将火两次Page_Load事件。

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.

页面名称/ VAR1:将仅在Page_Load事件一旦

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

,而

页面名称/ VAR1 / VAR2:将触发Page_Load事件两次

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

*多种额外的增值分销商仍然将仅在Page_Load事件两次*。

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

下面是从文件code snippits,第一个是的MapPageRoute,坐落在Global.asax:

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
        }
    );

接下来就是我已下令在菜单项的网页的方式,这是UL标签内的列表项:

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;
        };
    }

注意,那个时候我只是击中页面,它使用的默认值项目时,重载不会发生。

任何帮助或指导,任何人都可以提供将非常AP preciated!

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

编辑:用户控件只添加到页面一次。我通过把一个断点在Page_Load事件中测试了装载顺序 - 添加额外的路由时,只命中两次

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:再次感谢那些谁已经帮助了迄今为止 - 我仍然无法找到的双重负荷的原因 - 没有任何人有任何建议。

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 /答:下面的回答解释了如何解决这个问题 - 基本上是从最初的站点创建/删除包含任何../到JavaScript文件的引用。我希望这可以帮助其他人谁都有这个问题!

在预先感谢

保罗·赫特森

推荐答案

尝试过的一些事情,我已经发现,如果任何文件收到了../(在包括任何页面上),它会导致URL路由上述问题。

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.

只要确保没有文件是使用../解决了这个问题完全引用 - 特别是它是被造成问题的JavaScript文件

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

这篇关于使用URL路由,当用户控件asp.net多的Page_Load事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:56