动态控制,更新面板和事件 - 实现结果的最佳方式

时间:2022-01-19 15:59:06

Been pulling my hair out and doing a bit of looking on the web to try and figure out an elegant solution to my issue.

一直在拉我的头发,并在网上做一些尝试,找出一个优雅的解决方案来解决我的问题。

I have a ProductImages.aspx page. It shows all of the images associated with that product in a dynamically created list. Events are wired up to each picture to allow you to update it.

我有一个ProductImages.aspx页面。它在动态创建的列表中显示与该产品关联的所有图像。事件连接到每张图片以允许您更新它。

This works fine.

这很好用。

However, I have an option at the end which lets me add a new image. This is a button which fires off a call to the AddImage method.

但是,我在最后有一个选项,可以让我添加一个新图像。这是一个触发AddImage方法调用的按钮。

Now what is happening is that the original controls are being create and added to the page with events. Then the button event if fired which recreates all of the existing image controls and a new one. Add this point the new image control create after the OnInit does not have events attached due to the events being added AFTER the OnInit.

现在发生的事情是创建原始控件并将其添加到包含事件的页面。然后触发按钮事件,重新创建所有现有图像控件和新图像控件。添加此点后,由于在OnInit之后添加事件,OnInit没有附加事件后创建新的图像控件。

I can do a Response.Redirect to reload the page and fire the OnInit to wire up the events again but this seems very inelegant and destroys the point of using update Panels.

我可以做一个Response.Redirect重新加载页面并激活OnInit再次连接事件,但这似乎非常不优雅,破坏了使用更新面板的重点。

Any ideas?

2 个解决方案

#1


I'm thinking you could always load the picture upload control in a div and have a Javascript link to toggle the display attribute of the div.

我想你总是可以在div中加载图片上传控件,并有一个Javascript链接来切换div的显示属性。

Or perhaps use CollapsiblePanels from the AjaxToolKit to hide and show the upload form.

或者也许使用AjaxToolKit中的CollapsiblePanel来隐藏和显示上传表单。

I think either of those ways would be more elegant than doing a post back (even if it's in an UpdatePanel) just to retrieve the picture upload form.

我认为这些方法中的任何一种都比回帖(即使它在UpdatePanel中)更优雅,只是为了检索图片上传表单。

#2


Your questions makes it sound like you're saying that you can't put the controls in OnInit because it is only fired on the first load of the page. This is not the case - OnInit is fired each time the page is loaded (including postbacks), so you can re-create your controls there even when using an update panel.

你的问题听起来好像你说你不能把控件放在OnInit中,因为它只在第一次加载页面时触发。情况并非如此 - 每次加载页面时都会触发OnInit(包括回发),因此即使使用更新面板,也可以在那里重新创建控件。

One property that is different between the initial load and the postbacks is the Page.IsPostback property, which you can use to just perform actions on the first load of the page.

初始加载和回发之间不同的一个属性是Page.IsPostback属性,您可以使用该属性在第一次加载页面时执行操作。

#1


I'm thinking you could always load the picture upload control in a div and have a Javascript link to toggle the display attribute of the div.

我想你总是可以在div中加载图片上传控件,并有一个Javascript链接来切换div的显示属性。

Or perhaps use CollapsiblePanels from the AjaxToolKit to hide and show the upload form.

或者也许使用AjaxToolKit中的CollapsiblePanel来隐藏和显示上传表单。

I think either of those ways would be more elegant than doing a post back (even if it's in an UpdatePanel) just to retrieve the picture upload form.

我认为这些方法中的任何一种都比回帖(即使它在UpdatePanel中)更优雅,只是为了检索图片上传表单。

#2


Your questions makes it sound like you're saying that you can't put the controls in OnInit because it is only fired on the first load of the page. This is not the case - OnInit is fired each time the page is loaded (including postbacks), so you can re-create your controls there even when using an update panel.

你的问题听起来好像你说你不能把控件放在OnInit中,因为它只在第一次加载页面时触发。情况并非如此 - 每次加载页面时都会触发OnInit(包括回发),因此即使使用更新面板,也可以在那里重新创建控件。

One property that is different between the initial load and the postbacks is the Page.IsPostback property, which you can use to just perform actions on the first load of the page.

初始加载和回发之间不同的一个属性是Page.IsPostback属性,您可以使用该属性在第一次加载页面时执行操作。