简体   繁体   English

在asp.net上使用ajax创建动态控件

[英]creating dynamic controls using ajax on asp.net

I have button Add and Remove on my page. 我的页面上有添加和删除按钮。 Add button adds one checkbox, two textboxes and one dropdownlist to a new line on my page. “添加”按钮会在我的页面上的新行中添加一个复选框,两个文本框和一个下拉列表。 Remove button removes them. 删除按钮将删除它们。 I have this running nicely by following Joe Stagner's example . 按照Joe Stagner的例子,我很好地运行了这个。

Problem: The controls that are created dynamically all need to fire the same event when checked (for checkboxes), also for selected index changes (for dropdownlists). 问题:动态创建的控件都需要在选中时(对于复选框)触发相同的事件,也用于选定的索引更改(对于下拉列表)。

I have tried to add event handler when I create an object but it doesn't seem to fire? 我在创建对象时尝试添加事件处理程序,但它似乎没有触发?

I think you're probably running into the fact that your page, upon each page post, is being completely recreated - essentially the page has to duplicate what controls were on your page before it can attempt to feed postback (and events) to them. 我想你可能会遇到这样一个事实:你的页面在每个页面发布时都被完全重新创建 - 实际上页面必须复制页面上的控件,然后才能尝试向它们提供回发(和事件)。 I think what you probably need to do is add code to your page_load which will re-create the dynamically created controls, with the same ids as they had, and register the event handler. 我认为您可能需要做的是向page_load添加代码,该代码将重新创建动态创建的控件,使用与它们相同的ID,并注册事件处理程序。

Sounds like you have a page life cycle issue. 听起来你有一个页面生命周期问题。

For a dynamically created controls to fire events you should create them in the PreInit event of the page. 对于动态创建的触发事件的控件,您应该在页面的PreInit事件中创建它们。

Here's a link to a cheat sheet for Asp.net page life cycle. 这是Asp.net页面生命周期的备忘单链接。

you need to persist the dynamically created controls in some way [session, viewstate, etc.] for each page load. 你需要以某种方式[session,viewstate等]为每个页面加载持久保存动态创建的控件。 Recreate the dynamic controls and re-bind the events using delegates on each page load in preInit function. 在preInit函数中的每个页面加载上使用委托重新创建动态控件并重新绑定事件。

yeah, like what all said, it is Life cycle issue. 是的,就像所说的一样,这是生命周期问题。 when you load user controls dynamically you should always do the following. 当您动态加载用户控件时,应始终执行以下操作。

  • Assign a unique ID for each User Control. 为每个用户控件分配唯一的ID。
  • Reload the user controls on Page_Load or Page_Init Events. 重新加载Page_Load或Page_Init Events上的用户控件。

and to make it all easier i suggest to abstract the loading to a function that you will call from Page_Load and Page_Init as mentioned before, this function will check if hte target user control was loaded and will load it again for you, to do that, you store the loaded user controls IDs in Session or viewstate. 并且为了让我更容易,我建议将加载抽象为你将从之前提到的Page_Load和Page_Init调用的函数,这个函数将检查是否加载了目标用户控件,并将为你再次加载它,为此,您在Session或viewstate中存储加载的用户控件ID。

hope this helps. 希望这可以帮助。

If you want to do it without auto post back you can remove the auto post back and throw and ASP button on there. 如果你想在没有自动回发的情况下这样做,你可以删除自动回发和抛出和ASP按钮。 Any runat server should fire off your dynamic event handlers. 任何runat服务器都应该启动动态事件处理程序。

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

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