Text on an Image button in c# asp.net 3.5

后端 未结 9 1156
情话喂你
情话喂你 2020-12-03 17:52

I have a image button. I wanted to add a text \"Search\" on it. I am not able to add it because the \"imagebutton\" property in VS 2008 does not have text control in it. Can

9条回答
  •  孤街浪徒
    2020-12-03 18:36

    I realise this is an old post, but I have recently solved this same problem for myself.

    I created an ImgButton server control to render this:

    
    

    Using CSS to style a background image has some drawbacks, mainly:

    • The text tends to overlap the image unless you get clever with left-aligned image and right-aligned text (which is then inconvenient if you throw a RTL language into the mix)
    • The image is a background image, and does not appear to be "on the button" when we click the button it does not get "pushed down" the same as the text

    I'll try to insert the code here, but also have the full source code and examples here: Button with Img tag and Caption Text

    ImgButton.cs:

    [DefaultProperty("Text")]
    [DefaultEvent("Click")]
    [ToolboxData("<{0}:ImgButton runat=server>")]
    public class ImgButton : WebControl, IPostBackEventHandler
    {
        #region Public Properties
    
        public enum ImgButtonStyle
        {
            Button,
            Anchor
        }
    
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }
    
            set
            {
                ViewState["Text"] = value;
            }
        }
        [EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor))]
        public string ImgSrc { get; set; }
        public string CommandName { get; set; }
        public string CommandArgument { get; set; }
        [EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
        public string NavigateUrl { get; set; }
        public string OnClientClick { get; set; }
        public ImgButtonStyle RenderStyle { get; set; }
        [DefaultValue(true)]
        public bool UseSubmitBehavior { get; set; }
        [DefaultValue(true)]
        public bool CausesValidation { get; set; }
        public string ValidationGroup { get; set; }
        [DefaultValue(0)]
        public int Tag { get; set; }
    
        #endregion
    
        #region Constructor
    
        public ImgButton()
        {
            Text = "Text";
            ImgSrc = "~/Masters/_default/img/action-help.png";
            UseSubmitBehavior = true;
            CausesValidation = true;
        }
    
        #endregion
    
        #region Events
    
        // Defines the Click event.
        public event EventHandler Click;
        public event CommandEventHandler Command;
    
        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
    
        protected virtual void OnCommand(CommandEventArgs e)
        {
            if (Command != null)
            {
                Command(this, e);
            }
            RaiseBubbleEvent(this, e);
        }
    
        public void RaisePostBackEvent(string eventArgument)
        {
            if (CausesValidation)
            {
                Page.Validate(ValidationGroup);
                if (!Page.IsValid) return;
            }
            OnClick(EventArgs.Empty);
            if (!String.IsNullOrEmpty(CommandName))
                OnCommand(new CommandEventArgs(CommandName, CommandArgument));
        }
    
        #endregion
    
        #region Rendering
    
        // Do not wrap in  tag
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("");
        }
    
        protected override void RenderContents(HtmlTextWriter output)
        {
            string click;
            string disabled = (Enabled ? "" : "disabled ");
            string type = (String.IsNullOrEmpty(NavigateUrl) && UseSubmitBehavior ? "submit" : "button");
            string imgsrc = ResolveUrl(ImgSrc ?? "");
    
            if (String.IsNullOrEmpty(NavigateUrl))
                click = Page.ClientScript.GetPostBackEventReference(this, "");
            else
                if (NavigateUrl != null)
                    click = String.Format("location.href='{0}'", ResolveUrl(NavigateUrl));
                else
                    click = OnClientClick;
    
            switch (RenderStyle)
            {
                case ImgButtonStyle.Button:
                    if (String.IsNullOrEmpty(NavigateUrl) && UseSubmitBehavior)
                    {
                        output.Write(String.Format(
                            "",
                            ClientID,
                            disabled,
                            CssClass,
                            type,
                            UniqueID,
                            ToolTip,
                            imgsrc,
                            Text
                        ));
                    }
                    else
                    {
                        output.Write(String.Format(
                            "",
                            ClientID,
                            disabled,
                            CssClass,
                            type,
                            UniqueID,
                            click,
                            ToolTip,
                            imgsrc,
                            Text
                        ));
                    }
                    break;
    
                case ImgButtonStyle.Anchor:
                    output.Write(String.Format(
                        "\"{4}\"/{6}",
                        ID,
                        disabled,
                        CssClass,
                        click,
                        ToolTip,
                        imgsrc,
                        Text
                    ));
                    break;
            }
        }
    
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("");
        }
    
        #endregion
    }
    

    Here is the CSS I use on my buttons (where I put "icon" in the CssClass property of the button):

    button.icon
    {
        cursor: pointer;
    }
    
    button.icon img
    {
        margin: 0px;
        padding: 0px 5px 0px 5px;
        vertical-align: middle;
    }
    

提交回复
热议问题