How come when I put this code in my page to request the query string my page is returning a href of nothing?
Code In Page:
<a href='<% String.Format("book.aspx?id=" + Request.QueryString["id"]); %>'>Test</a>
Result:
<a href=''>Test</a>
开发者_运维技巧
You need to use <%=
and not <%
to output the result of the String.Format to the page. Right now you are just discarding the result.
I'm not sure why you have the String.Format in there, all you need is this:
<a href="book.aspx?id=<%=Request.QueryString["id"] %>">Test</a>
Also, even though ASP.NET checks for potentially dangerous request values, it's good practice to either encode or validate values like this:
<a href="book.aspx?id=<%=Server.HtmlEncode(Request.QueryString["id"]) %>">Test</a>
or
<a href="book.aspx?id=<%=Convert.ToInt32(Request.QueryString["id"]) %>">Test</a>
精彩评论