Using a Bootstrap Glyphicon on an ASP.NET Button

Is there a way to utilize glyphicons as icons for ASP Buttons using C# instead of HTML?

Normally, you would apply the proper glyphicon CSS to an <i> element and then nest it under the desired button. But I want to accomplish this programmatically.

I attempted the following code without success:


HtmlGenericControl i1 = new HtmlGenericControl("i");
i1.Attributes["class"] = "glyphicon glyphicon-backward";
i1.Attributes["aria-hidden"] = "true";

Button btnFirst = new Button();
btnFirst.ID = "btnFirst";
btnFirst.CommandName = "FirstPage";
btnFirst.CssClass = "btn btn-default pageButton";
btnFirst.Click += new EventHandler(btnFirst_Click);
btnFirst.Controls.Add(i1);
container.Controls.Add(btnFirst);

When running the code above, the button appears but with no icon. There is also no nested <i> element in the HTML source.

Answer №1

Essentially, the issue stems from the fact that <asp:Button> cannot be paired with glyphicons because when used, it transforms into <input type='button'>, which does not allow for any child elements to be included.

There are two potential solutions:


Option 1: Utilize <asp:LinkButton> in place of <asp:Button>:

<asp:LinkButton runat="server" ID="lbFoo" OnClick="lbFoo_Click" CssClass="btn btn-default">
    <i class="glyphicon glyphicon-backward" aria-hidden="true"></i>Back
</asp:LinkButton>

This is a commonly employed solution for this issue.


Option 2: Use <button> to trigger a hidden <asp:Button>

<button type="button" class="btn btn-default" onclick="<%=btnHidden.ClientID %>.click()">
    <i class="glyphicon glyphicon-backward" aria-hidden="true"></i>
    Click
</button>

<asp:Button runat="server" style="display:none;" ID="btnHidden"
    OnClick="btnHidden_Click"></asp:Button>

This workaround can be utilized if there is a preference for using <asp:Button> over <asp:LinkButton>. While there are other workarounds available, this method is considered most legitimate.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Are you able to identify the contrast between my coding and the tutorial?

Can someone explain the differences between the code in my top navigation here and the one in this tutorial? Aside from changing the menu id name, I've used the same HTML and CSS. Oddly, the menu on my site is not visible; it seems to be hidden someh ...

What is the process for generating a list of Razor Pages in Blazor?

I'm currently working on creating a list of razor pages. To clarify, in my "Index.razor" file, I have the following: @page "/" @inject IJSRuntime JSRuntime <h1 style="text-align:center;color:red">Hey :)</h1> <br /> <Counter/> ...

Arranging H1 Styling with CSS for Optimum Alignment

My issue revolves around a DIV tag adorned with a background image: <a href="#"> <div class="pagebar jquery"> <h1>JQuery Demonstrations</h1> </div> </a> The CSS applied to this element is as follows: .pagebar ...

Printed pages featuring neatly organized two-column div layout

Forgive me if this question has already been asked, but I need some guidance: I am designing reports using HTML and CSS which are then converted into PDF format. The report requires a two-column layout on most pages, so I am using: <div class="two_col ...

Once the AutoReset property is set, the previously paused System.Timers.Timer begins to fire the Elapsed

Encountered an unusual behavior with System.Timers.Timer: The timer is set with a 1-second interval and AutoReset configured as false. After starting the timer, the Elapsed event occurs in one second. Inside the event handler, timer.Enabled turns to fals ...

Content of two li tags in row and column are not aligned properly

I am struggling with ensuring that the two columns and two rows of text in my page section are always aligned correctly. Column 1 and the contents of row 1 should be on the same line, but instead, column 2 is appearing a line or two below column 1. This is ...

Problem with the `file` input not working properly

Currently, I am working on creating a form with two input fields - one for text and the other for file upload. However, the issue I am facing is that the Browse button for the file input is being displayed inside the input box. My goal is to have the butto ...

In search of tutorials on UI and working thread management

In my daily work, I utilize VSTS 2008, C#, and .Net 3.5. I am eager to delve into the distinctions between the UI thread and worker thread, as well as explore other types of threads. Are non-UI threads the same as worker threads? I am also interested in un ...

Modify a value using jQuery

On my Asp.net webform, I have a label called "LabelTotalNumber". I am looking to update the value of this label every minute using jQuery without having to refresh the form. How can I achieve this? Currently, I am able to accomplish updating the value onc ...

Identifying when a .NET application is starting up in a cold or hot state

My goal is to analyze the startup time of my .net WPF desktop application. However, I've noticed that the startup time can vary significantly between cold and hot starts. I'm interested in separating these times so that I can gather more accurate ...

Getting a callback from a button located within a TableHeader to a UiViewController in Xamarin.iOS

How can I send a callback to my UIViewController when a button inside TableHeader is clicked? I am primarily writing code in C#, but open to using Swift if necessary. ...

Creating a stylish design: integrating a progress bar within a card using Bootstrap 5

I'm currently delving into the world of bootstrap and I'm curious about how to incorporate a progress bar into a card layout to enhance my design skills. I experimented with inline-block and inline display properties, but they didn't yield t ...

Enable UserControl to fill the entire screen

As I work on creating a reusable UserControl with various features, one of the requirements is to include a full-screen mode. The challenge now is figuring out how to achieve this functionality. In most cases, it's preferable for the container form ...

Switching the background color of a button on click and resetting the color of any previously clicked buttons (a total of 8

I'm struggling to implement a feature where only one button out of a column of 8 should be toggled yellow at a time, while the rest remain default green. Unfortunately, I can't seem to get the function to execute on click, as none of the colors a ...

RestSharp does not produce any response when used in IIS

I've been struggling with this issue for quite some time. My asp.net project involves using RestSharp to send a request to an authorization server in order to check if a user can log in to the site. Below is the method I use to call the authorizatio ...

Guide to creating a responsive layout using CSS and HTML

I am looking to position the main contents (content 1 and content 2) in between two side menus on my exercise page. At the moment, the main contents are overlapping with the two menus. Can someone guide me on how to fix this alignment issue? Thank you. ...

Placement issue with image in Bootstrap card on mobile devices

My Bootstrap card has an image in the second column that looks great on desktop, but not so much on mobile. <div class="mt-5 container card w-100"> <div class="row"> <div class="col"> <div class="card-body"> ...

When a dynamic control is created on a page, I must retrieve the value upon button click

There have been many controls generated and I want to save the value on button click which is dynamically generated on the page. Can anyone help me with this? I am new to this field so I'm using many types of code, but it can be challenging. When we ...

The CSS tag combination of 'position:relative;' and 'top:xx%;;' functions properly in Chrome, however, it does not work in any non-webkit browsers such as Firefox, Internet Explorer, Opera, etc

Encountering some strange behavior with a simple piece of code that I can't seem to figure out! I am trying to position a footer navigation at the bottom of my page using 'position:relative;' and it works perfectly in Chrome. However, in FF ...

Retrieving Date and Time from MySQL database with C# and AllowZeroDatetime flag

Having some trouble with my MySQL connection in C#. I'm using the AllowZeroDatetime option, but when I query my table, the date comes back as all zeros while the time is 12:00:00 AM. How can I resolve this issue? Here's my connection string: str ...