How to use a Twitter Bootstrap icon inside an ASP.NET Button?

后端 未结 5 1763
感动是毒
感动是毒 2021-02-04 09:22

How can I use a:

 

with an asp.net button?

相关标签:
5条回答
  • 2021-02-04 09:23

    LinkButton, like this...

    <asp:LinkButton ID="SelectButton" runat="server" CssClass="btn btn-info"><i class="icon-ok icon-white"></i>&nbsp;Select</asp:LinkButton>
    
    0 讨论(0)
  • 2021-02-04 09:28

    Thanks Anders Smedman, your code sure did the job. here is the C# code if anyone needs it.

     private void FixGlyph(PlaceHolder ph, Button btn, string iconClass, string customLabelStye = "")
        {
            if (btn.Visible)
            {
                var g = new HtmlGenericControl();
                g.ID = "labelFor_" + btn.ID;
                g.TagName = "label";
                g.Attributes.Add("for",btn.ClientID);
                g.Attributes.Add("class","" + btn.CssClass +"");
                if (customLabelStye != "")
                {
                    g.Attributes.Add("style",customLabelStye);
                }
                g.InnerHtml = "<i class='" + iconClass + "'></i> " + btn.Text;
                ph.Controls.Add(g);
                btn.Attributes.Add("style","display:none;");
            }
    
        }
    
    0 讨论(0)
  • 2021-02-04 09:29

    try this

    <asp:LinkButton ID="btnExample" runat="server" Text="<span class='glyphicon glyphicon-repeat'></span> Button" CssClass="btn btn-primary btn-xs" OnClick="btn_Click"></asp:LinkButton>
    

    or

    <asp:LinkButton ID="btnExample" runat="server" Text="<i class='glyphicon glyphicon-flash'></i> Button" CssClass="btn btn-primary btn-xs" OnClick="btn_Click"></asp:LinkButton>
    

    Regards C:

    0 讨论(0)
  • 2021-02-04 09:38

    I have done like this.

    Markup:

    <asp:PlaceHolder ID="phButtonToLabelsAdminBox" runat="server"></asp:PlaceHolder>
    <asp:Button ID="btnSave" runat="server" CssClass="btn" Text="Spara" />
    <asp:Button ID="btnClear" runat="server" CssClass="btn" Text="Töm/Ny" />
    

    CodeBehind Page_Load()

    FixGlyph(phButtonToLabelsAdminBox, btnSave, "icon-ok")
    FixGlyph(phButtonToLabelsAdminBox, btnClear, "icon-refresh")
    

    And the Sub:

    Private Sub FixGlyph(ph As PlaceHolder, btn As Button, IconClass As String, Optional CustomLabelStyle As String = "")
    
    If btn.Visible = False Then Exit Sub
    Dim g As New HtmlGenericControl
    g.ID = "labelFor_" + btn.ID
    g.TagName = "label"
    g.Attributes.Add("for", btn.ClientID)
    g.Attributes.Add("class", "" + btn.CssClass + "")
    If Not CustomLabelStyle = "" Then g.Attributes.Add("style", CustomLabelStyle)
    g.InnerHtml = "<i class=""" + IconClass + """></i> " + btn.Text
    ph.Controls.Add(g)
    btn.Attributes.Add("style", "display:none;")
    
    End Sub
    

    I use ordinary asp:Button in my Markup and the only things are that to run the FixGlyph after other code that might set visible true/false for the buttons and to add the FixGlyph in the order you want the buttons to appear. Other than that, it works for me.

    0 讨论(0)
  • 2021-02-04 09:40

    You may add the <i> tag as a value to the LinkButton Text attribute.

    e.g.

    <asp:LinkButton ID="btnExcluir" runat="server" Text="<i aria-hidden='true' class='icon-remove-3'></i>" CssClass="btn btn-danger" />
    

    You can even use it with a side text.

    e.g.

    <asp:LinkButton ID="btnExcluir" runat="server" Text="Link Name&nbsp;<i aria-hidden='true' class='icon-remove-3'></i>" CssClass="btn btn-danger" />
    
    0 讨论(0)
提交回复
热议问题