在href中,从后面的代码绑定时〜不起作用

时间:2013-04-12 05:41:50

标签: asp.net

当从后面的代码绑定href标签时它不起作用。这是我的aspx代码。当我直接将<a>标记放在aspx上时它正在工作,而一旦我通过Response.write执行它就会读取〜而不是作为根目录。

<% Response.Write("<a id=\"A1\" runat=\"server\" class=\"menuItem\" href=\"~/HR/Emp/EmpDetails.aspx?mid=167\">My PIPs</a> "); %> 

<a id="A1" runat="server" class="menuItem" href="~/HR/Emp/EmpDetails.aspx?mid=167">My PIPs</a> 

通过文字绑定也无法正常工作

Literal.text = "<a id=\"A1\" runat=\"server\" class=\"menuItem\" href=\"~/HR/Emp/EmpDetails.aspx?mid=167\">My PIPs</a> "; 

请帮忙。

these are the HTML tags
<ul id="jMenu">
    <li>
      <a  runat="server" class="fNiv" href="~\Default.aspx?mid=1&">Home</a> 
      </li>
    <li>
      <a  runat="server" class="fNiv" href="">Human Resources</a> 
    <ul>
      <li class="arrow" /> 
       <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\PIPWorkFlow.aspx?mid=166">Performance Improvement Plan (PIP)</a> 
    <ul>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\PIPWorkFlow.aspx?mid=171">Data Protection Statement</a> 
      </li>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\EmpPIPDetails.aspx?mid=167">My PIPs</a> 
      </li>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\PIPHome.aspx?mid=168">Team PIPs</a> 
      </li>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\HRPIPHistory.aspx?mid=169">Employee PIPs</a> 
      </li>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\AppraisalRatingUpload.aspx?mid=170">Upload Appraisal Ratings</a> 
      </li>
    <li>
      <a runat="server" class="menuItem" href="~\HR\EMP\PIPRoleAssignment.aspx?mid=472">PIP Spoc Role Assignment</a> 
      </li>
      </ul>
      </li>
      </ul>
      </li>
    <li>
      <a runat="server" class="fNiv" href="~\ContactUs.aspx?mid=39">Contact Us</a> 
      </li>
      </ul>

这些是html标签......如果我们用Literal或div或TD绑定这些标签......他们正在阅读〜符号...... 如果我将这些标签直接放在HTML中,它的工作非常精细

1 个答案:

答案 0 :(得分:3)

Response.WriteLiteral.Text会输出您提供给它们的任何内容而无需处理。

这意味着无论你给出什么,这些方法/属性都将逐字输出。不会发生URL解析,runat=server属性毫无意义。

您可以使用Control.ResolveClientUrl() method自行生成链接,或在链接代码中将链接作为服务器控件进行操作。

选项1

// this line is fine
string url = ResolveClientUrl("~/some-url/foo.aspx");

// this line is quite ugly, but should work
var lit = new Literal { Text = "<a href='" + url + "'>a link</a>" };

// add it the page/control's control hierarchy
this.Controls.Add( lit );

选项2

// instantiate the control dynamically
// URL resolution is done automatically
var lnk = new HtmlAnchor { HRef = "~/some-url/foo.aspx" };

// add it the page/control's control hierarchy
this.Controls.Add( lnk );

选项3

假设您必须处理字符串,而不是控制层次结构(根据注释)。这并不理想,但如果字符串中的URL相对于应用程序根目录,则可能会这样做。

// resolve the application root
string root = ResolveClientUrl("~/");

// string of raw HTML
string html = "<a href=\"~/HR/Emp/EmpDetails.aspx?mid=167\">My PIPs</a>";

// replace the urls with a resolved version
// this is very simplistic...a proper parser would be more reliable
html = html.Replace( "~/", root + "/" );

// output as desired
Response.Write( html );