locked
server tag is not well formed RRS feed

  • Question

  • User1997074450 posted

    The server tag is not well formed. This is in masterpage.
     
    <li><a runat="server" href="~/Products?userId='" <%: Session("userid") %> "'" > Products </a></li>

    Saturday, October 17, 2020 12:57 AM

Answers

  • User409696431 posted

    <%:  is not valid.

    Is there any reason you need a runat=server in that link?  If you do it would be simpler to use a hyperlink and construct the href in code behind. (Or set the HtmlAnchor HRef in code behind).

    But a standard HTML link will work:

    <a href="/Products?userId=<%= Session["userid"].ToString() %>" > Products </a>

    If you are using VB.net, use () instead of [] for Session.

    I assume you are checking if Session is null in code behind and setting the value to something valid, since doing this in the .aspx page doesn't check for that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 17, 2020 2:20 AM

All replies

  • User409696431 posted

    <%:  is not valid.

    Is there any reason you need a runat=server in that link?  If you do it would be simpler to use a hyperlink and construct the href in code behind. (Or set the HtmlAnchor HRef in code behind).

    But a standard HTML link will work:

    <a href="/Products?userId=<%= Session["userid"].ToString() %>" > Products </a>

    If you are using VB.net, use () instead of [] for Session.

    I assume you are checking if Session is null in code behind and setting the value to something valid, since doing this in the .aspx page doesn't check for that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 17, 2020 2:20 AM
  • User409696431 posted

    Just adding, if you really want to use an HtmlAnchor and bind the Session value to the HRef in the .aspx page, you can do it, but its more work than simply using a standard link, as I showed above.

    First, you need to make sure your page has

    using System.Web.UI.HtmlControls;

    Second, you have to find, then databind, the Anchor in code behind.  An example below, but the actual FindControl you would need depends on where this link is nested in your page structure.

    protected void Page_Load(object sender, EventArgs e)           
            {
                HtmlAnchor prodlnk = (HtmlAnchor)(Page.Form.FindControl("MainContent").FindControl("ProductLink"));
                prodlnk.DataBind();
                ......

    And the format for the link in the .aspx page would be:

    <a runat="server" ID="ProductLink" HRef=<%# "~/Products?userId=" + Session["userid"].ToString() %> >Products</a>

    I'm using C#.  You'll need to modify your syntax if you are using VB.net.

    Saturday, October 17, 2020 3:39 AM