Problems with preprocessor directive for ImgUrl property of Image in ASP.NET
I would like to parse QueryString and put the ID value to the ImgUrl path, 开发者_C百科I am trying this code:
<asp:Image ID="imgImageNormal" runat="server" ImageUrl='<%# string.Format("ImageHandler.ashx?ID={0}",Request.QueryString["ID"].ToString()) %>'/>
But the produced result is none. I am not getting any error message but after viewing the source of the page, this is the output for the image:
<img id="ctl00_ContentPlaceHolder1_imgImageNormal" src="" style="border-width:0px;" />
What am I doing wrong?
Is this within the context of binding? If not, the <%#...%>
syntax will not work. This code works:
<img ID="imgImageNormal" src=<%=string.Format("ImageHandler.ashx?ID={0}",Request.QueryString["ID"].ToString())%> />
Note setting runat="server"
or enclosing the src attributes in quotes will cause this to fail. Although the above works, you would probably be better off just setting the imageurl property of an asp:image control from the codebehind on the page_load event.
For reference on where to use asp.net inline tags check out this site: http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
精彩评论