简体   繁体   中英

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:

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:

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

You're essentially creating a Url to the address ItemsForSale.aspx. Prefix your URL with ~/ to have it recognized as a route relative to your base url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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