动态添加的事件处理程序不点火 [英] Dynamically Added Event Handler Not Firing

查看:138
本文介绍了动态添加的事件处理程序不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个快速code片段,这似乎并没有在所有的工作对我来说。我从文件中读取创建单选按钮列表。问题是,当一个单选按钮,单击该事件处理程序我在code不火成立。我一遍遍在调试模式下换行,没有运气测试它... ...所有。我在这里缺少一些东西明显????

在先进的感谢!

  strLine中= strLine.Trim();
 System.Diagnostics.Debug.WriteLine([3-A]按日期船 - 日期:+ strLine中); 尝试{shipByDate =(Convert.ToDateTime(strLine中)); }
 赶上(例外五){shipByDate =新的DateTime(); } shipByDesc = sr.ReadLine()修剪()。
 System.Diagnostics.Debug.WriteLine([3-B]按日期船 - 降序:+ shipByDesc); 单选按钮=新的单选按钮();
 button.Text = shipByDesc + - + shipByDate.ToString(MM / DD / YYYY);
 button.Checked = FALSE;
 button.GroupName =shipByOptions;
 button.ID =shipByRadio+计数; //button.EnableViewState = TRUE;
 button.AutoPostBack = TRUE;
 button.CheckedChanged + =新的EventHandler(shipBy_CheckedChanged); //< - 不工作! //form1.Controls.Add(button);
 shipByPlaceHolder.Controls.Add(按钮);


解决方案

您需要连接到它会触发事件之前添加在每个回发的按钮。

如果你想想看一会儿,它才有意义 - 如果按钮尚未创建(上回发),那么是可以不触发按钮事件。按钮的必须的存在之前附加了事件可被解雇了。

的OnInit 页面事件是最合适的地方动态控件添加到页面。

阅读有关 asp.net页面生命周期

Here is a quick code snippet, that doesn't seem to work at all for me. I'm reading from a file to create a list of radio buttons. The problem is that when one of the radio buttons is clicked the Event Handler I have set up in the code doesn't fire. I have tested it over and over in debug mode with line breaks... all with no luck. Am I missing something obvious here????

Thanks in Advanced!

 strLine = strLine.Trim();
 System.Diagnostics.Debug.WriteLine("[3-a] ship by date - date: " + strLine);

 try{ shipByDate = (Convert.ToDateTime(strLine)); }
 catch (Exception e) { shipByDate = new DateTime(); }

 shipByDesc = sr.ReadLine().Trim();
 System.Diagnostics.Debug.WriteLine("[3-b] ship by date - desc: " + shipByDesc);

 RadioButton button = new RadioButton();
 button.Text = shipByDesc + " - " + shipByDate.ToString("MM/dd/yyyy");
 button.Checked = false;
 button.GroupName = "shipByOptions";
 button.ID = "shipByRadio" + count;

 //button.EnableViewState = true;
 button.AutoPostBack = true;
 button.CheckedChanged += new EventHandler(shipBy_CheckedChanged); // <-- doesn't work!!!

 //form1.Controls.Add(button);
 shipByPlaceHolder.Controls.Add(button);

解决方案

You need to add the button on every postback before events attached to it will fire.

If you think about it for a moment, it will make sense - if the button has not been created (on the postback), then there are no button events that can fire. The button must exist before events attached to it can be fired.

The OnInit page event is the most suitable place to add dynamic controls to a page.

Read about the asp.net page life cycle.

这篇关于动态添加的事件处理程序不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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