动态占位符控制和连接事件 [英] Dynamic Placeholder Control and attaching an Event

查看:136
本文介绍了动态占位符控制和连接事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些大规模的头痛在一个非常动态的应用程序的工作。

I have been getting some massive head aches working on a very dynamic app.

我使用的是动态的占位符控件来源:

I am using a dynamic placeholder control from:

http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

这尝试了回发后重新创建动态创建控件时,为我节省了很多的麻烦。

This saves me a lot of hassle when trying to re-create dynamically created controls after a postback.

任何人都有的任何问题与事件处理程序连接到CheckBox控件?

Has anyone else had any issues with attaching a event handler to checkbox control?

下面是我的code为动态创建的复选​​框。

Here is my code for the dynamically created checkbox.

// Now I create my checkbox
chkDynamic = new CheckBox();
string chk = "chk";

// Add it to our dynamic control
chkDynamic.CheckedChanged += new EventHandler(chkDynamic_CheckedChanged);
chkDynamic.ID = chk;
DynamicControlsPlaceholder1.Controls.Add(chkDynamic);
chkDynamic.Text = "hey";

这工作,但它像该事件没有得到连接!
这里是我的事件处理程序!

This works, but its like the event is not getting attached! Here is my event handler!

protected void chkDynamic_CheckedChanged(object sender, EventArgs e)
    {

    if (((CheckBox)sender).Checked)
        Response.Write("you checked the checkbox :" + this.chkDynamic.ID);
    else
        Response.Write("checkbox is not checked");
}

现在,如果我是用一个常规的占位符或面板这一伟大工程。

Now if I was to use a regular placeholder or a panel this works great.

防爆。改变这一行:

DynamicControlsPlaceholder1.Controls.Add(chkDynamic);

Panel1.Controls.Add(chkDynamic);

和它完美的作品。

可能有人请告诉我,这是一个问题与此对照,还是我的编码?

Could someone please tell me, is this an issue with this control, or my coding?

有没有错误,那是发生了意想不到的唯一的事情是,当我使用我的DynamicControlsPlaceholder事件不会开火。

There are no errors, the only thing that is happening that is unexpected is my event is not firing when I'm using the DynamicControlsPlaceholder.

推荐答案

如果您要添加动态控件,你必须创建/不迟于的OnInit()重新创建控件 。这是在.NET页面生命周期,其中的视图状态和事件恢复的地步。如果仅仅是为了增加您使用的是动态占位符控件,然后简单地把控件创建/休闲的OnInit动态控制的目的()将解决你的问题。给它一个尝试,让我知道你的结果。

If you are adding dynamic controls, you MUST create/recreate the controls no later than OnInit(). This is the point in the .NET page lifecycle where the viewstate and events are restored. If it is solely for the purpose of adding dynamic controls that you are using the dynamic placeholder control, then simply putting the control creation/recreation in OnInit() will solve your problem. Give it a try and let me know your results.

这篇关于动态占位符控制和连接事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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