代码之家  ›  专栏  ›  技术社区  ›  Skillzore

c#如何在“处理”按钮事件时更改按钮图标/格式?

  •  1
  • Skillzore  · 技术社区  · 9 年前

    我有几个服务器 HtmlButton

    HtmlButton button = new HtmlButton();
    button.ID = idString + "_btnStart";
    button.InnerHtml = "<i class=\"fa fa-play btn-green\"/></i>";
    button.Attributes.Add("type", "button");
    button.Attributes.Add("runat", "server");
    button.Attributes.Add("class", "btn btn-link btn-xs");
    button.Attributes.Add("title", "Start");
    button.ServerClick += new EventHandler(BtnStart_Click);
    ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(button);
    

    每个按钮都有自己的 UpdatePanel :

    UpdatePanel uPanel = new UpdatePanel()
    {
        ID = idString + "_uPanel",
        UpdateMode = UpdatePanelUpdateMode.Conditional
    };
    uPanel.ContentTemplateContainer.Controls.Add(button);
    

    现在,当点击按钮时,我想首先更改 InnerHtml 在 Page_Load

    private void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Control control = null;
            string controlName = Request.Params.Get("__EVENTTARGET");
            if (controlName != null && controlName != string.Empty)
            {
                control = FindControl(controlName);
                if(control != null)
                {
                    HtmlButton button = (HtmlButton)control;
                    UpdatePanel uPanel = (UpdatePanel)button.Parent.Parent;
                    string id = button.ID.Split('_')[1];
                    switch (id)
                    {
                        case "btnStop":
                            button.InnerHtml = "<i class=\"fa fa-spinner fa-pulse btn-red\"/></i>";
                            break;
                        case "btnStart":
                            button.InnerHtml = "<i class=\"fa fa-spinner fa-pulse btn-green\"/></i>";
                            break;
                        case "btnRestart":
                            button.InnerHtml = "<i class=\"fa fa-refresh fa-spin btn-blue\"/></i>";
    
                            break;
                        default:
                            break;
                    }
                    uPanel.Update();
                }
            }
        }
    }
    

    然后在这种情况下,在完成后将其更改回:

    private void BtnStart_Click(object sender, EventArgs e)
    {
        HtmlButton button = (HtmlButton)sender;
        Debug.WriteLine("Button clicked: " + button.ID);
    
        // sleeping to simulate process stuffs
        Thread.Sleep(2000);
    
        button.InnerHtml = "<i class=\"fa fa-play btn-green\"/></i>";
        (button.Parent.Parent as UpdatePanel).Update();
    }
    

    有没有更“标准”的方法来做这件事,或者这样可以吗?

    UpdatePanel uPanel = (UpdatePanel)button.Parent;
    

    给我一个错误:

    • $exception{“无法将类型为”System.Web.UI.Control“的对象强制转换为类型为”System.Web.UI.UpdatePanel“。System.InvalidCastException

    button.Parent 什么都做不了。我需要打电话 (button.Parent as UpdatePanel).Update(); 在里面 页面加载 为了让它发挥作用。建议?

    需要呼叫:

    UpdatePanel uPanel = (UpdatePanel)button.Parent.Parent;
    

    因为第一个家长是 ContentTemplateContainer .

    fa-spinner 然后回来。我的意图是,在上述情况下,它应该显示 fa旋转器

    编辑2:

    页面加载 和按钮事件。所以即使 InnerHtml

    有疑问我们可以创建一个变通方法来触发事件两次吗?第一次更改图标并执行工作,第二次将图标更改回来?其他想法?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Amr Elgarhy    9 年前

    正如您所说,由于页面周期,您将无法看到页面加载和按钮单击事件之间的图标变化。

    因此,我建议在本例中使用一点Javascript,正如@Mauricio在他的评论中所建议的那样。

    您可以添加新的客户端事件 OnClientClick="showIcon"

    function showIcon()
    {
       document.getElementById('#myBtnId').innerHTML = <i class='fa fa-spinner fa-pulse btn-red'/></i>"
    }
    

    当然,你需要在javascript中添加你的登录名,因为它不像我写的那么简单,但我只是想告诉你我将如何解决这个问题。

    推荐文章