将 asp.net ImageButton 扩展为 css Sprite

发布于 2024-08-14 05:53:33 字数 1261 浏览 7 评论 0原文

我正在将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滴情不沾 2024-08-21 05:53:34

@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

我为君王 2024-08-21 05:53:33

或者,您可以将 ImageButton 替换为 LinkBut​​ton,并在其中包含一个 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

白鸥掠海 2024-08-21 05:53:33

ImageButton 的属性 ImageUrl 中,您可以放置​​一个指向透明图像(大小为 1x1 像素,例如:spacer.png)的链接。
然后在 CSS 中放置一个具有宽度和高度的背景图像。

因此,在任何使用 ImageButton 的地方,您都可以调用相同的透明图像(保留在浏览器缓存中)。

In the property ImageUrl of your ImageButton, 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文