I created a simple HTML Button within c# as
Button btn = new Button();
btn.ID="myID";
btn.Click+=new EventHandler(someFunc);
which works perfectly fine as expected. But all I need to do is to insert an icon within it.
Similar to something like this.
add
Could someone please guide as to how to insert this within the button?
Talk1:
Why are you adding i element within button? Instead use btn.style = "font-style:italic;" and btn.text = "add"; to add the text and italic style to the button, add the required class material-icons with the button itself
Solutions1
Instead of using Button you have to use HtmlButton and have server click event, because WebControls Button will render like
Also someFunc should be event not string
Following is the complete code that you can use
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Web.UI.HtmlControls.HtmlButton btn = new System.Web.UI.HtmlControls.HtmlButton();
btn.ID = "myID";
btn.Attributes["class"] = "mdl-button mdl-js-button mdl-button--fab mdl-button--colored";
btn.ServerClick += new EventHandler(someFunc);
btn.InnerHtml = "add";
// you can add your button to some other parent control or to form
myCustomPanel.Controls.Add(btn);
}
}
protected void someFunc(object sender, EventArgs e)
{
// your code
}
Make sure to add your button in form or some other control inside form.
Solutions2
You can create a HtmlGenericControl object and add all the relevant properties like InnerText,class etc and finally add this object to the button's controls collection. This will give you the expected output:-
HtmlGenericControl iTag = new HtmlGenericControl("i");
iTag.InnerText = "Add";
iTag.Attributes["class"] = "material-icons";
btn.Controls.Add(iTag);