开发者

QueryString Link Help

开发者 https://www.devze.com 2023-02-16 20:16 出处:网络
How come when I put this code in my page to request the query string my page is returning a href of nothing?

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消