0

I have this in my asp.net page:

<% foreach (Product item in ProductList())
           {
            %>
          <div id="i<%:item.CODE %>" class="item" dir="rtl">
               <label class="code"><%:item.CODE %></label>
       <asp:HyperLink runat="server" ID="DetailsLink" Text="Details" 
       NavigateUrl="~/Details.aspx?ProductCode=<%:item.CODE %>" font-size="18px"></asp:HyperLink>
          </div>
         <% } %>

Although the first <%:item.CODE%> works and I can see the item's code written,

in the NavigateUrl string it does not work.

I get a link to "http://localhost:34546/Details.aspx?ProductCode=<%:item.CODE %>"

How can I concat the item's code to the link?

2 Answers 2

1

Try this

 <% foreach (String str in new string[] { "Apple", "Mango", "Orange" })
       {
    %>
    <div id="i<%: str %>" class="item" dir="rtl">
        <label class="code">
            <%: str %></label>
               <a href='<%: "Details.aspx?ProductCode=" + str %>'>DetailsLink</a>
    </div>
    <% } %>

instead of

<asp:HyperLink runat="server" 
       ID="DetailsLink" 
       Text="Details" 
       NavigateUrl="~/Details.aspx?ProductCode=<%:item.CODE %>" 
       font-size="18px"/>
Sign up to request clarification or add additional context in comments.

Comments

1

You should use repeater control & handle its ItemDataBound event. That will make you code cleaner & easy to debug.

in your .aspx.cs file:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                Product item = e.Item.DataItem as Product ;
                HyperLink DetailsLink = e.Item.FindControl("DetailsLink") as HyperLink;
                DetailsLink.NavigateUrl= "~/Details.aspx?ProductCode=" + item.CODE;

            }
        }



protected void Page_Load(object sender, EventArgs e)
        {
            rpt.DataSource = ProductList();
            rpt.DataBind();
        }

in your .aspx file:

  <asp:Repeater ID="rpt" runat="server">                                                   <ItemTemplate>                                                            
     <asp:HyperLink runat="server" ID="DetailsLink" Text="Details" 
        font-size="18px"></asp:HyperLink>                             
  </ItemTemplate>
 </asp:Repeater>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.