<a href> & <asp:Hyperlink>

R

rn5a

Consider the following code:

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim sqlReader As SqlDataReader

sqlReader = 'calling a function that returns SqlDataReader

dlCart.DataSource = sqlReader
dlCart.DataBind
End Sub
</script>
<form runat="server">
<asp:DataList ID="dlCart" runat="server">
<HeaderTemplate>
<table>
<tr>
........
........
........
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Container.DataItem("ProdID") %></td>
<td><%# Container.DataItem("ProductName") %></td>
<td><%# Container.DataItem("Price") %></td>
<td><a href='RemoveItem.aspx?ProdID=<%# Container.DataItem("ProdID")
%>'>Remove</a></td>
</tr>
</ItemTemplate>
......
</asp:DataList>
</form>

Assume that the DataList displays 3 products to a user. The ProdIDs of
the 3 products are 10, 20 & 30 respectively.

When the user clicks the product whose ProdID is 10, as expected, he is
taken to RemoveItem.aspx with the querystring ProdID=10 appended to it
i.e. the user is taken to

http://myserver/RemoveItem.aspx?ProdID=10

But instead of using <a href...> for rendering the 'Remove' link, if I
use <asp:Hyperlink>

<td><asp:Hyperlink ID="lnk" NavigateUrl='RemoveItem.aspx?ProdID=<%#
Container.DataItem("ProdID") %>' Text="Remove" runat="server"/></td>

then <%# Container.DataItem("ProdID") %> doesn't get assigned to the
appropriate ProdID. Rather, the URL of link literally remains as what
is coded above (using <asp:Hyperlink..>). In other words, irrespective
of whichever 'Remove' link a user clicks, he is taken to

http://myserver/RemoveItem.aspx?ProdID=<%#%20Container.DataItem("ProdID")%20%>

How do I use <%# Container.DataItem("ProdID") %> in the NavigateUrl
property of <asp:Hyperlink> in order to assign the corresponding
database ProdID value to the querystring ProdID so that, for e.g. if
the user clicks the 'Remove' link corresponding to the product whose
ProdID is 10, he is taken to

http://myserver/RemoveItem.aspx?ProdID=10
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top