将 asp.net ImageButton 扩展为 css Sprite
我正在将我的 asp.net 应用程序上的 ImageButtons 更新为扩展 ImageButton 的 WebControl,我将其称为 CSSImageButton。我已经重写了渲染方法,这样我就可以让它们与 css 精灵一起使用。我已经设法使 OnClick 和 OnClientClick 正常工作。但有些按钮有 OnCommand,我不知道该怎么做。
public class CSSImageButton : ImageButton
{
public string DivClass { get; set; }
public CSSImageButton()
{
CssClass = "";
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.Write("<div class=\"" + CssClass + " " + DivClass + "\">");
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<a id=\"" + this.ClientID + "\" title=\""+ this.ToolTip+"\"");
if (!String.IsNullOrEmpty(OnClientClick))
{
writer.Write(" href=\"javascript:void(0);\" OnClick = \"" + OnClientClick + "\"");
}
else
{
writer.Write(" href=\"javascript:" + this.Page.GetPostBackEventReference(this, "ButtonClick") + "\"");
}
writer.Write("\"></a>");
}
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.Write("</div>");
}
}
I'm updating my ImageButtons on my asp.net application to a WebControl that extends the ImageButton, which I called CSSImageButton. I've overridden the render methods so I can make them work with css sprites. I've managed to get the OnClick working and the OnClientClick. But some buttons have the OnCommand, and I can't figure out what to do.
public class CSSImageButton : ImageButton
{
public string DivClass { get; set; }
public CSSImageButton()
{
CssClass = "";
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.Write("<div class=\"" + CssClass + " " + DivClass + "\">");
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<a id=\"" + this.ClientID + "\" title=\""+ this.ToolTip+"\"");
if (!String.IsNullOrEmpty(OnClientClick))
{
writer.Write(" href=\"javascript:void(0);\" OnClick = \"" + OnClientClick + "\"");
}
else
{
writer.Write(" href=\"javascript:" + this.Page.GetPostBackEventReference(this, "ButtonClick") + "\"");
}
writer.Write("\"></a>");
}
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.Write("</div>");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@roman m:这种方法的问题(取决于您的情况)是,这最终会将 div 嵌套在锚点(“bar”)内,而该锚点将无法通过 HTML 验证。
当然,如果这不是问题,那么您的建议将解决问题,但是从标记/验证的角度来看,艾蒂安的解决方案更加“自然”。
只是我的 $.02
@roman m: The issue with this approach (depending on your situation), is that this will end up nesting a div within an anchor ("bar") which will not pass HTML validation.
Granted, if that is not a concern, then what you suggest will solve the problem, however Etienne's solution is more "natural" from a markup / validation perspective.
Just my $.02
或者,您可以将 ImageButton 替换为 LinkButton,并在其中包含一个 div 并使用适当的 css-sprites 类
来源
Alternatively, you can replace ImageButton with LinkButton, and have a div inside of it with proper class for css-sprites
The Source
在
ImageButton
的属性ImageUrl
中,您可以放置一个指向透明图像(大小为 1x1 像素,例如:spacer.png)的链接。然后在 CSS 中放置一个具有宽度和高度的背景图像。
因此,在任何使用
ImageButton
的地方,您都可以调用相同的透明图像(保留在浏览器缓存中)。In the property
ImageUrl
of yourImageButton
, you could place a link to a transparent image (which has a size of 1x1 pixel, for example: spacer.png).Then in your CSS you place a background image on it, with a width and height.
So at any place where you use an
ImageButton
, you call the same transparent image (which remain in the browsers cache).