简体   繁体   English

动态创建ascx并向其中添加控件

[英]Creating ascx dynamically and adding controls to it

I am a beginner in asp.net(C#) and stuck in an important point. 我是asp.net(C#)的初学者,但有一个重点。

I have a dropdown list on my homepage which users select a category. 我的主页上有一个下拉列表,用户可以选择一个类别。 After selecting the category, user will fill a form which has related controls to that category in it. 选择类别后,用户将填写一个表单,该表单中具有与该类别相关的控件。

As I have many categories, I just want to have single ascx page and adding controls to it dynamically according to the user choice. 因为我有很多类别,所以我只想拥有一个ascx页面,并根据用户的选择向其动态添加控件。

For example: One chose Telephone category, he will face a form having drop down lists asking, what brand? 例如:选择电话类别,他将面对一个带有下拉列表的表格,询问什么品牌? what color? 什么颜色? And one chose, book category, he will face drop down lists asking which type? 一个人选择了书籍类别,他将面对下拉列表,询问哪种类型? howmany pages? 多少页?

So 1 ascx must do my work at runtime done as I have alot of categories. 所以1 ascx必须在运行时完成我的工作,因为我有很多类别。

I am going to take these criterias from a database table which has CategoryID and Criteria colomns. 我将从具有CategoryID和Criteria列的数据库表中获取这些条件。

And if I can do that, will it be possible to add field validators to these dynamically created controls. 如果我可以这样做,是否可以将字段验证器添加到这些动态创建的控件中。

Nearly all controls are drop down list, if this helps. 如果有帮助,几乎所有控件都在下拉列表中。

Any help would be highly appreciated.. 任何帮助将不胜感激。

Thanks alot 非常感谢

You can add an asp:placeholder Or literal control to your page and then populate all the controls programatically. 您可以在页面上添加一个asp:placeholder或文字控件,然后以编程方式填充所有控件。 Altough you will have to take special care to make sure the control state and view state are correct at all times. 不过,您必须特别小心以确保控件状态和视图状态始终正确。

Here's an example on how to do it 这是一个如何做的例子

http://www.4guysfromrolla.com/articles/081402-1.aspx http://www.4guysfromrolla.com/articles/081402-1.aspx

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

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