回发后,通过后面的代码添加到更新面板的项目不会触发事件

时间:2021-05-01 20:38:28

There is an edit button on the page upon landing in an updatepanel. User clicks edit button, the event that fires removes this button from the container and adds a save button (in the updatepanel). This function also attaches an event to the new button.

登录到更新面板时,页面上有一个编辑按钮。用户单击编辑按钮,触发的事件将从容器中删除此按钮并添加保存按钮(在updatepanel中)。此功能还会将事件附加到新按钮。

New button shows up on the page but clicking on it does not fire the event. Obviously event does not register. There is probably a trick to make this happen in the updatepanel but I could not figure it out. Does anybody know the answer?

新按钮显示在页面上但单击它不会触发事件。显然事件没有注册。可能有一个技巧可以在updatepanel中实现这一点,但我无法弄明白。有人知道答案吗?

Thanks in advance...

提前致谢...

2 个解决方案

#1


When you add controls dynamically, you must regenerate them on every postback, other way the events won't be fired.

当您动态添加控件时,必须在每次回发时重新生成它们,否则不会触发事件。

You must regenerate the controls on the page_init event.

您必须在page_init事件上重新生成控件。

I will do it like this:

我会这样做:

i) write a "generateControl" function to generate the control.

i)编写“generateControl”函数来生成控件。

ii) call this function when you want to generate it and set a flag (a public variable to true)

ii)当你想要生成它并设置一个标志(公共变量为true)时调用此函数

iii) on page_init check if the flag is true and call the "generateControl" function if it is.

iii)在page_init上检查标志是否为真,如果是,则调用“generateControl”函数。

#2


Is there a reason you can't have both the add and save buttons on the page, with the save button's Visible property set to false except when an add is going on? That way, you get to take advantage of ViewState (i.e. your button will keep visibility between page loads) for very mild cost.

有没有理由你不能在页面上同时添加和保存按钮,保存按钮的Visible属性设置为false,除非正在进行添加?这样,您可以充分利用ViewState(即您的按钮可以在页面加载之间保持可见性),而且成本非常低。

#1


When you add controls dynamically, you must regenerate them on every postback, other way the events won't be fired.

当您动态添加控件时,必须在每次回发时重新生成它们,否则不会触发事件。

You must regenerate the controls on the page_init event.

您必须在page_init事件上重新生成控件。

I will do it like this:

我会这样做:

i) write a "generateControl" function to generate the control.

i)编写“generateControl”函数来生成控件。

ii) call this function when you want to generate it and set a flag (a public variable to true)

ii)当你想要生成它并设置一个标志(公共变量为true)时调用此函数

iii) on page_init check if the flag is true and call the "generateControl" function if it is.

iii)在page_init上检查标志是否为真,如果是,则调用“generateControl”函数。

#2


Is there a reason you can't have both the add and save buttons on the page, with the save button's Visible property set to false except when an add is going on? That way, you get to take advantage of ViewState (i.e. your button will keep visibility between page loads) for very mild cost.

有没有理由你不能在页面上同时添加和保存按钮,保存按钮的Visible属性设置为false,除非正在进行添加?这样,您可以充分利用ViewState(即您的按钮可以在页面加载之间保持可见性),而且成本非常低。