通过内容页面上的 DropDownList 更改动态控件
问题:带有带有 UpdatePanel 和占位符的向导控件的内容页面。 UpdatePanel 上方是一个 DropDownList。当用户更改下拉列表中的选择时,我需要在下拉列表下方显示不同的输入控件。当用户单击向导控件上的“下一步”时,我还需要能够从这些动态控件中获取数据。
我知道所有动态控件都必须在 OnInit 方法中创建,以便在回发期间从这些控件中获取数据。但是,当触发下拉列表的 SelectedIndexChanged 事件时,将调用 OnInit 方法...然后调用 PageLoad...最后调用 SelectedIndexChanged 事件的处理程序。 ViewState 直到 OnInit 和 OnInit 之后很久才恢复。 PageLoad 方法已被调用,因此无法知道调用 OnInit 时用户在列表框中选择了什么...这正是我需要创建动态控件时的情况。
那么...你如何解决这个问题呢?您是否只需使用 JavaScript 编写整个页面或大部分页面?
提前致谢。
Problem: Content Page with Wizard Control with UpdatePanel and Placeholder. Above the UpdatePanel is a DropDownList. I need to display different input controls below the drop-down list when the user changes the selection in the drop-down list. When the user clicks 'Next' on the wizard control, I need to be able to get the data out of those dynamic controls as well.
I know all the dynamic controls have to be created in the OnInit method in order to get the data back from those controls during the postback. However, when the drop-down list's SelectedIndexChanged event is fired, the OnInit method is called... then the PageLoad... and finally the handler for the SelectedIndexChanged event is called. ViewState hasn't been restored until well after the OnInit & PageLoad methods have been called, so there is no way to know what the user chose in the list box at the time OnInit is called... which exactly when I'm required to create the dynamic controls.
So... how do you solve this problem? Do you just have to write the entire page, or most of it, using JavaScript?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于使用老式方法来满足此类要求。我将编写更新面板中所需的所有控件,并将其 Visible 属性设置为 false。然后,在回发时读取下拉列表的状态并将适当的控件 Visible 属性设置为 true。这样就没有“动态”控件,并且由于 Visible 属性为 false 的控件不会呈现,因此在用户看到它们之前不会下载它们。
I tend to use an old school method for this type of requirement. I would write all of the controls that are needed in the update panel, with their Visible property set to false. Then, on post back read the drop down's state and set the approperate controls Visible property to true. This way there is no "dynamic" controls, and due to the fact that controls whose Visible property is false are not rendered, they are not downloaded until the user should see them.
您还可以使用 asp:hiddenfield 并将值设置为您在心里创建的 case var。然后在上面运行一个小的 jQuery 脚本来查看,
然后只需创建一个函数,例如:
然后可以通过获取标签来完成任何要显示/隐藏的项目:
或显示,无隐藏。
我在工作中使用了这个,因为有时您需要在不触发回发的情况下进行更改,但在他们参与表单时仍然收集数据。
为了代码的强度和安全性,我通常在许多事件中避免使用 jQuery,但有时使用 jQuery 来操作 DOM 元素会容易得多。
you can also use an asp:hiddenfield and set the value to a case var you mentally create. then run a small jQuery script on top to look at
then just make a function, for instance:
then any item to show/hide can be done with getting the tag:
or display, none to hide.
I used this at work because at times you need to change without firing the postback, but still collect data when they engage the form.
I typically avoid jQuery for many events for strength of code and security, but DOM element manipulation can be much easier at times with jQuery.