在Page_Load中动态添加一个控件,如下所示:dim Con as System.Web.UI.UserControl = Page.LoadControl("control.ascx")Con.Attributes.Add("test","ok")PlaceHolder1.Controls.add(con)上面第二行为这个控件添加了一个名为test的属性,但是这样只能添加字符串,如果现在属性是保存在一个数组或者结构里面的,有没有办法将这个数组或结构直接传递给控件?

解决方案 »

  1.   

    不好意思,没时间写具体的例子,我从手头找到的一本书摘一段话(节选自《ASP.NET.Unleashed 2nd.Edition》第29章第2节,中文书名应该叫《ASP.NET揭秘(第二版)??》) 书里面附了具体的例子Creating DataBound Controls
    You can bind several of the standard ASP.NET controls to a data source. For example, you can bind a Repeater control to data sources, such as DataReaders, ArrayLists, and DataViews.
    You too can create a control that supports binding to a data source. To create a DataBound control, you must do the following:1> Add a DataSource property. This property typically implements the IEnumerable, ICollection, IList, or IListSource interface.
    2> Add an OnDataBinding subroutine. This subroutine is invoked when the DataBind method is called.