【发布时间】:2011-10-02 15:36:56
【问题描述】:
我有一个 UserControl AutomatedFeedGridView.ascx,它本质上是一个 GridView。它有一个公共属性Category,使用控件在页面上传递。
我的问题是我想根据调用页面上的下拉列表进行过滤。
下面是AutomatedFeedGridView 控件的代码隐藏:
// The feed category
public Feeds.FeedCategory Category { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<AutomatedFeed> x = Feeds.GetAutomatedFeed(Category);
gvAutomatedFeed.DataSource = x;
gvAutomatedFeed.DataBind();
}
else
{
List<AutomatedFeed> x = (List<AutomatedFeed>)gvAutomatedFeedCategory.DataSource;
foreach (AutomatedFeed y in x)
{
// if condition is not met, hide y
}
}
所以在第一次加载时,GridView 被绑定到AutomatedFeed 对象的列表。在任何后续调用(由包含控件的页面上的回发引起)我想运行一些代码来过滤掉 GridView 中的一些项目。问题是这一行:
List<AutomatedFeed> x = (List<AutomatedFeed>)gvAutomatedFeedCategory.DataSource;
我已经尝试了所有here 的解决方案,但它们似乎都不起作用,我总是收到 Object reference not set to an instance 错误。我是否遗漏了某些东西,或者我这样做的方式完全错误?
我知道我可以轻松地再次调用 Feeds.GetAutomatedFeed(Category),但一定有比再次调用存储过程更好的方法吗?
【问题讨论】:
标签: c# .net gridview user-controls postback