How to use a Twitter Bootstrap icon inside an ASP.

2020-05-23 08:58发布

问题:

How can I use a:

<i class="icon-etc"></i> 

with an asp.net button?

回答1:

LinkButton, like this...

<asp:LinkButton ID="SelectButton" runat="server" CssClass="btn btn-info"><i class="icon-ok icon-white"></i>&nbsp;Select</asp:LinkButton>


回答2:

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" />


回答3:

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:



回答4:

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.



回答5:

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;");
        }

    }