简体   繁体   English

ASP.NET Img标签

[英]ASP.NET Img tag

I have a problem with how ASP.Net generates the img tag. 我对ASP.Net如何生成img标签有问题。 I have a server control like this: 我有一个这样的服务器控件:

<asp:Image runat="server" ID="someWarning" ImageUrl="~/images/warning.gif" AlternateText="Warning" />

I expect it to generate this: 我希望它会生成此:

<img id="ctl00_ContentPlaceHolder1_ctl00_someWarning" src="../images/warning.gif" />

but instead it generates this: 但它会生成以下内容:

<img alt="" src="/Image.ashx;img=%2fimages%2fwarning.gif"</img>

This give me errors when I execute the following js: 当我执行以下js时,这给了我错误:

document.getElementById('ctl00_ContentPlaceHolder1_someWarning')

Any idea why it won't generate the expected html? 知道为什么它不会生成预期的html吗?

Looks like it's trying to use a custom handler (ashx) to deliver the image. 看起来它正在尝试使用自定义处理程序(ashx)交付图像。 Do you have any additional modules that may be overriding the default behaviour of the asp:Image? 您是否还有其他模块可能会覆盖asp:Image的默认行为?

Your JavaScript won't work because the image tag has not been given an ID in the HTML that was generated. 您的JavaScript无法使用,因为在生成的HTML中没有为图片标签赋予ID。

You can get the actual ID that is generated by using ClientID. 您可以获取使用ClientID生成的实际ID。 I use this to get the ID of a control for use in JavaScript using syntax similar to the following: 我使用它来获取要在JavaScript中使用的控件ID,其语法类似于以下内容:

document.getElementById('<%=ddlCountry.ClientID%>').style.display  = "block";

However you can also use it in your code-behind to get the same thing. 但是,您也可以在代码背后使用它来获得相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM