本文介绍了从ASP.NET用户控件的相对路径NavigateUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个GridView的用户控件。 GridView控件既有HyperLinkField字段列,并包含一个超链接控制模板列。

I have a user control that contains a GridView. The GridView has both a HyperLinkField column and a template column that contains a HyperLink control.

ASP.NET项目的结构如下,在每种情况下使用用户控制Default.aspx页面。

The ASP.NET project is structured as follows, with the Default.aspx page in each case using the user control.


  • 应用程序根目录

    • 控制

      • 用户控件与GridView控件


      • 的Default.aspx

      • Edit.aspx


      • 的Default.aspx

      • Edit.aspx


      • 的Default.aspx

      • Edit.aspx

      请注意:正在使用的文件夹,确保用户具有正确的角色

      Note: The folders are being used to ensure the user has the correct role.

      我需要能够以设置HyperLinkField字段和NavigateUrl为超链接DataNavigateUrlFormatString解析为Edit.aspx页中的相应文件夹中。

      I need to be able to set the DataNavigateUrlFormatString for the HyperLinkField and the NavigateUrl for the HyperLink to resolve to the Edit.aspx page in the corresponding folder.

      如果我设置的导航网址为Edit.aspx在浏览器中的网址显示为
      'HTTP://应用程序根目录/控制/ Edit.aspx不管始发目录

      If I set the navigate URL to "Edit.aspx" the URL in the browser appears as'http://Application Root/Controls/Edit.aspx' regardless of the originating directory.

      作为路径必须是相对于当前页面,而不是应用程序根目录。

      I can't use the Web application root operator (~/) as the path needs to be relative to the current page, not the application root.

      我如何使用在多个文件夹相同的用户控制和化解URL到另一页在同一文件夹?

      How can I use the same user control in multiple folders and resolve the URL to another page in the same folder?

      注意:这个问题是基于强烈关闭的我的问题相匹配。

      Note: The question is strongly based off a similar question by azhar2000s on the asp.net forums that matches my problem.

      推荐答案

      虽然打字这个问题,我碰到一个可能的解决方案来,并进一步修改这个从@Thomas使用反馈。

      While typing up this question I came across one possible solution and have further modified this using feedback from @Thomas.

      修改<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.control.ap$p$plativetemplatesourcedirectory.aspx\"相对=nofollow> Control.Ap prelativeTemplateSourceDirectory 改变由控制产生的相对路径。我已经将其设置为的当前请求夹。现在,在该用户的任何相对路径与相对于被请求的页面,而不是用户控制路径。

      Changing Control.AppRelativeTemplateSourceDirectory alters the relative paths produced by the control. I've set it to a root relative virtual path for the current requests folder. Now any relative paths in the UserControl with be relative to the requested page rather than the user controls path.

      //Page Load Event for the User Control
      protected void Page_Load(object sender, EventArgs e)
      {
          string rootPath = HttpContext.Current.Request.ApplicationPath;
          if (!rootPath.EndsWith("/"))
          {
              rootPath += "/";
          }
      
          Uri requestUri = HttpContext.Current.Request.Url;
          string folderPath = requestUri.AbsolutePath.Remove(0, rootPath.Length);
          string lastSegment = requestUri.Segments[requestUri.Segments.Length - 1];
          folderPath = folderPath.Remove(folderPath.LastIndexOf(lastSegment));
      
          AppRelativeTemplateSourceDirectory = "~/" + folderPath;
      }
      

      这篇关于从ASP.NET用户控件的相对路径NavigateUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:41