简体   繁体   English

html输入按钮服务器单击事件未触发

[英]html input button server click event not fire

I have the following code inside the CS file of the page, i am trying to add an html button inside a div in the server side to close the alert modal dialog. 我在页面的CS文件中包含以下代码,我试图在服务器端的div内添加html按钮以关闭警报模式对话框。 When I add the event to the button and try to fire it. 当我将事件添加到按钮并尝试触发它时。 The event doesn't fires. 该事件不会触发。 So, where is the problem ? 那么,问题出在哪里呢?

 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); } 

Can you help me ? 你能帮助我吗 ?

The button probably doesn't exist at the time when the server callback is executing. 在执行服务器回调时,该按钮可能不存在。

You are using a dynamically created button. 您正在使用动态创建的按钮。 In order for it to be able to trigger a server side method, the button needs to be added and have it's event handler bound in an early stage of the page life cycle, such as On_Init. 为了使其能够触发服务器端方法,需要添加按钮,并在页面生命周期的早期阶段绑定事件处理程序,例如On_Init。

We don't know when you call your "Alert" method, but it might very well too late. 我们不知道何时调用您的“警报”方法,但这可能为时已晚。 It also needs to be called on every postback - or the link between button and handler won't be there when the button causes a postback. 还需要在每次回发中调用它-否则当按钮导致回发时,按钮和处理程序之间的链接将不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM