添加动态控件
我有一个页面,需要根据用户在页面上的表单上所做的选择来构建,然后显示一组不同的问题供他们回答。
假设用户在页面编辑的表单中选择原因A,那么它需要在页面edit_confirmation上显示问题1和2。但如果他们选择原因 B,则需要显示问题 3 和问题 4。
我从查询字符串中获取原因代码并设置了 switch 语句,但我找不到如何输出不同控件的任何地方。如果应该显示问题 1 和 2,则一个可以是文本框,另一个可以是复选框,但如果应该显示问题 3 和 4,则一个可能需要是下拉列表和复选框。
编辑:我将尝试以下一些建议,然后回来标记答案并相应地投票。感谢大家的快速回复。
编辑编辑:rlb.usa 和 AndrewVos 的答案同样有效。我选择了安德鲁的,因为这似乎是更“正确”的做法。
I have a page I need to build out where depending on the selection the user made on a form on the page prior it displays a different set of questions for them to answer.
So say the user selects Reason A from the form on page edit, then it needs to display Questions 1 and 2 on page edit_confirmation. But if they select Reason B then it needs to display Questions 3 and 4.
I'm grabbing the reason code from the query string and have a switch statement set up, but I can't find anywhere how to output different controls. If Questions 1 and 2 are supposed to show up, one could be a text box and the other a checkbox, but if questions 3 and 4 are supposed to show up one may need to be a dropdown list and a checkbox.
EDIT: I'm going to try some of the below suggestions and will be back to mark the answer and upvote accordingly. Thank you all for the quick response.
EDIT EDIT: Both rlb.usa and AndrewVos's answers worked equally well. I went with Andrew's since it seemed like the more "proper" way of doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看一下 MultiView 控件。它允许您添加视图并根据您的要求指定哪个视图可见。
Have a look at the MultiView control. It allows you to add Views and specify which one is visible according to your requirements.
输出不同的控件?哎哟,听起来很痛苦。我认为您正在寻找的术语是动态控件(在代码中创建的控件)。我在这方面遇到的麻烦总是多于它的价值。
最常见的做法是在窗体上设置所有控件。将每个“问题”或相关问题集全部包含在
ASP:Panel
中。接下来,在代码中,您所需要做的就是根据您的需要应用逻辑并隐藏/显示 ASP:Panel(通过设置 Visible 属性)。您可以在Page_Load
上执行此逻辑(如果适用),或者在更改特定答案时执行此逻辑。Output different controls? Ouch, that sounds very painful. I think the term you are looking for is dynamic controls (controls created within the code). I've always had more trouble with this than it's worth.
The most common practice way to do it is to set up all of your controls on the form. Have each "question" or relevant question sets all contained within an
ASP:Panel
. Next, inside your code, all you need to do is apply your logic and hide/show theASP:Panel
s (by setting theVisible
property) according to your needs. You can either do this logic onPage_Load
, if applicable, or when a particular answer was changed.您可以使用 PlaceHolder 控件并在服务器端向其添加相关控件,或者您可以将所有控件的 style.display 设置为 none 并使用客户端 javascript 将相关控件设置为“阻止”
You could use a PlaceHolder control and add the relevant controls to it on the server side, or you could render all of the controls with thier style.display set to none and set the relevant ones to "block" useing client side javascript
为什么不根据他们在页面编辑时选择的原因将他们发送到不同的页面?
Why not just send them to a different page based on the reason they select on page edit?
一种方法是静态添加控件。根据用户选择隐藏或取消隐藏控件。这种方法最容易实现,但有点丑陋。
另一种方法是动态创建控件并添加它们。您将不得不考虑管理视图状态,因为这些事情可能非常令人讨厌。
1 way would be to have the controls added statically. Based on the user selection hide or unhide the controls. This approach is the easiest to implement but is kind of ugly.
Other approach is to dynamically create controls and add them. You will have to think of managing viewstate as these things can be pretty nasty.
您可以简单地在 page_load 事件中设置控件的可见属性。因此,在 switch 语句中,您只需使您希望用户看到的控件可见。
You could simply set the visible property on the control in your page_load event. So in the switch statement, you would only make visible the controls that you want the user to see.