简体   繁体   English

创建唯一的URL时超链接不起作用? C#asp.net

[英]Hyperlink won't work when creating a unique url? C# asp.net

<asp:SqlDataSource ID="itemsforsale" runat="server"
     ConnectionString="<%$ ConnectionStrings:ElmtreeConnection  %>"
     SelectCommand="SELECT * FROM Products WHERE Products.CategoryId = @CategoryId">

     <SelectParameters>
         <asp:QueryStringParameter Name="CategoryId" 
              QueryStringField="CategoryId" Type="Int32" />
     </SelectParameters>
</asp:SqlDataSource>

<asp:HyperLink ID="hyperlink" runat="server" 
     NavigateUrl='<%# "ItemsForSale.aspx?CategoryId"+Eval("CategoryId") %>' 
     Text="Beauty"></asp:HyperLink>

This is my markup. 这是我的标记。 I'm not getting any errors when loading the page, however the link is not working. 加载页面时没有出现任何错误,但是链接不起作用。 Can anyone give me any insight as to why? 谁能给我任何关于为什么的见解?

An equal sign is missing in the URL: URL中缺少等号:

NavigateUrl='<%# "ItemsForSale.aspx?CategoryId=" + Eval("CategoryId") %>'

If the HyperLink is not in a databound control, you must call its DataBind method (or the DataBind method of the Page itself) in Page_Load to make sure that its databinding expression is evaluated: 如果HyperLink不在数据绑定控件中,则必须在Page_Load调用其DataBind方法(或Page本身的DataBind方法),以确保其数据绑定表达式被求值:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        hyperlink.DataBind();
        ...
    }
}

You're essentially creating a Url to the address ItemsForSale.aspx. 本质上,您是在创建到地址ItemsForSale.aspx的Url。 Prefix your URL with ~/ to have it recognized as a route relative to your base url. 在URL前面加上〜/,以将其识别为相对于基本URL的路由。

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

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