我在我的页面代码后面有一个自定义的类列表:
public List<Category> Categories = new List<Category>();现在,我在该页面上还有一个用户控件,它应该显示该列表。
如何从用户控件访问列表,或者,是否可以从页面直接在用户控件中创建列表?
我的用户控件代码:
public List<Category> Categories = new List<Category>();
protected void Page_Load(object sender, EventArgs e)
{
}
public class Category
{
public string category_id { get; set; }
public string category { get; set; }
}我的页面代码:
public List<Category> Categories = new List<Category>();
protected void Page_Load(object sender, EventArgs e)
{
Category MyCategory = new Category();
MyCategory.category_id = 1;
MyCategory.category = "sample";
Categories.Add(MyCategory);
}
public class Category
{
public string category_id { get; set; }
public string category { get; set; }
}发布于 2011-10-01 20:29:13
因为Hasan Khan已经开始帮助我了,但是从来没有回答我额外的问题。我必须找到我自己的解决方案。
我在最后的方法是不是将项目添加到页面的列表中,而是直接将它们添加到用户控件中,如下所示:
filterControl.Categories.Add(new widgets_filter.Category{ category_id = "", category = ""});我不知道这是不是一个好的解决方案,但至少它是有效的。
如果有人会提供一个更好的答案(使用示例代码),我会尝试使用它们,同时,这就是我所拥有的。
发布于 2011-10-01 20:04:57
在用户控件中创建一个属性,并将列表分配给该属性
MyUserControl.ListProperty = theList;或
将列表作为公共属性放在页面中,并通过user contorl中的page属性访问它。您需要首先将其转换为您的页面类型。
var theList = ((MyPage)Page).ListProperty或
将列表放入HttpContext.Current.Items,然后从那里获取它。
HttpContext.Current.Items["theList"] = theList;https://stackoverflow.com/questions/7620108
复制相似问题