添加事件处理程序,以动态创建的复选​​框(ASPX,C#) [英] adding an event handler to a dynamically created checkbox (aspx, c#)

查看:133
本文介绍了添加事件处理程序,以动态创建的复选​​框(ASPX,C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是,我想一个事件处理程序添加到动态创建的复选​​框。我已经看了看其他的方式来做到这一点,并决定建立包含我的CheckBox动态表对我来说是最好的选择。因为我需要手动管理的ViewState我还没有添加这些复选框控件树。无论哪种方式,我的code工作在不同的是我的CheckBox的CheckChanged事件不会触发各种方式。我在我的页面加载事件添加此事件处理我的复选框,但是,任何页面时我尝试似乎给了我同样的结果:

So my problem is that I want to add an event handler to a dynamically created CheckBox. I have already looked at other ways to do this, and decided that creating a dynamic table which contains my CheckBoxes is the best option for me. I have not added these CheckBoxes to the Control Tree because I need to manage the ViewState manually. Either way, my code works in every way except that my CheckBox's CheckChanged Event does not fire. I am adding this eventhandler to my CheckBox in my pageLoad event, however, any page event I try seems to give me the same results:

CheckBox chbxLv1 = new CheckBox();
chbxLv1.ID = "DymanicallyCreatedIDForIdentification";
chbxLv1.AutoPostBack = true;
chbxLv1.CheckedChanged += new EventHandler(this.checkChanged);

/* Way lower in my code */

protected void checkChanged(object sender, EventArgs e)
{
//Some code goes here which never seems to execute... grrr
}

我认为这可能与ViewState中首先一个问题,并做了相当多的研究上。现在我想我做了愚蠢的事情与添加事件处理程序。我不知道为什么这个事件永远不会触发,但我是一个小新在添加事件控制。难道我在这里需要一个委托?

I thought that this may be a problem with the ViewState at first and did quite a bit of research on that. I'm now thinking I am doing something dumb with adding an event handler. I'm not sure why this event never fires, but I'm a little new at adding events to a control. Do I need a delegate here?

- 罗马

推荐答案

为了对动态加载控件中的ASP.NET页面生命周期过程中处理不当,他们需要的OnInit过程中添加到页面(或之前LoadViewState ,真),否则它们的状态信息将不会被保留,你可以,事实上,腐败的视图状态取决于如何/在哪里在页面的控制图添加的东西。

In order for dynamically loaded controls to be handled properly during the ASP.NET Page Lifecycle, they need to be added to the page during OnInit (or prior to LoadViewState, really) otherwise their state information will not be maintained and you can, in fact, corrupt the viewstate depending on how/where things are added in the page's control graph.

这篇关于添加事件处理程序,以动态创建的复选​​框(ASPX,C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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