动态创建HTML按钮控件并在其中嵌套i标签 [英] Dynamically creating HTML button control and nesting i tag within it

查看:499
本文介绍了动态创建HTML按钮控件并在其中嵌套i标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中创建了一个简单的HTML按钮作为

  Button btn = new Button(); 
btn.ID =myID;
btn.Click + = new EventHandler(someFunc);

这可以像预期的那样完美地工作。但我需要做的就是在其中插入一个图标。
类似于类似的东西。

 < button class =mdl-button mdl-js-button mdl-按钮 -  fab mdl-button  -  colored> 
< i class =material-icons> add< / i>
< / button>

有人请指导如何在按钮中插入它吗?$ b $而不是使用按钮,您必须使用 HtmlButton $ c>并且有服务器点击事件,因为WebControls Button会呈现像< input type ,所以它不能有其他控件。



另外someFunc应该是event不是字符串



以下是您可以使用的完整代码

< pre $ 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 =< i class = \material-icons \> add< / i>;

//您可以将按钮添加到其他父控件或形成
myCustomPanel.Controls.Add(btn);



protected void someFunc(object sender,EventArgs e)
{
//您的代码
}

请确保在表单中添加您的按钮或其他控件。


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.

<button class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored">
  <i class="material-icons">add</i>
</button>

Could someone please guide as to how to insert this within the button?

解决方案

Instead of using Button you have to use HtmlButton and have server click event, because WebControls Button will render like <input type so it cannot have another control inside it.

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 = "<i class=\"material-icons\">add</i>";

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

这篇关于动态创建HTML按钮控件并在其中嵌套i标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆