【问题标题】:html input button server click event not firehtml输入按钮服务器点击事件不触发
【发布时间】:2018-09-19 20:10:41
【问题描述】:

我在页面的 CS 文件中有以下代码,我试图在服务器端的 div 内添加一个 html 按钮以关闭警报模式对话框。 当我将事件添加到按钮并尝试触发它时。该事件不会触发。 那么,问题出在哪里?

public Alert(HtmlGenericControl alert ,string alertMessage)
        {
            vAlert = alert;

            alert.Attributes.Add("class", "uk-modal");
            alert.Attributes.Add("aria-hidden", "true");
            alert.Attributes.Add("style", "display: none; overflow-y: scroll;");

            HtmlGenericControl innerDiv = new HtmlGenericControl();
            innerDiv.TagName = "div";
            innerDiv.Attributes.Add("class", "uk-modal-dialog");
            innerDiv.Attributes.Add("style", "top: 35.5px;text-align:center; padding:30px;");

            HtmlInputButton btnclose = new HtmlInputButton();
            btnclose.Attributes.Add("type", "button");
            btnclose.Attributes.Add("id", "alert_close");
            btnclose.Attributes.Add("runat", "server");
            btnclose.Attributes.Add("class", "uk-modal-close uk-close");
            btnclose.Attributes.Add("style", "padding:15px;");
            
            btnclose.ServerClick += new EventHandler(btnclose_ServerClick);
            innerDiv.Controls.Add(btnclose);
            

            HtmlGenericControl p = new HtmlGenericControl();
            p.TagName = "p";
            p.InnerText = alertMessage;
            innerDiv.Controls.Add(p);

            alert.Controls.Add(innerDiv);

            ShowAlert(alert);
        }

        private void btnclose_ServerClick(object sender, EventArgs e)
        {
            HideAlert(vAlert);
        }

你能帮帮我吗?

【问题讨论】:

  • 从 shtml/aspx 文件中添加带有 Button 部分的部分
  • 你能给我举个例子吗?
  • ¿ 您在哪个事件中添加控件?我的意思是页面生命周期。
  • 感谢您的推荐。但我试图在服务器端的按钮单击事件中显示/隐藏一个对话框,而不使用 javascript 或 jquery
  • @YusufShayah 您在什么事件中创建控件。你应该这样做Page_Init

标签: c# html asp.net


【解决方案1】:

在执行服务器回调时,该按钮可能不存在。

您正在使用动态创建的按钮。为了能够触发服务器端方法,需要在页面生命周期的早期阶段添加按钮并绑定其事件处理程序,例如 On_Init。

我们不知道您何时调用“警报”方法,但很可能为时已晚。它还需要在每次回发时调用 - 否则当按钮导致回发时,按钮和处理程序之间的链接将不存在。

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多