绑定后获取 GridView 中的项目
我有一个 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;
我已经尝试了此处的所有解决方案,但似乎都不起作用,我总是收到未将对象引用设置为实例错误。我是否遗漏了什么或者我以完全错误的方式做这件事?
我知道我可以轻松地再次调用 Feeds.GetAutomatedFeed(Category)
但一定有比再次调用存储过程更好的方法吗?
I have a UserControl AutomatedFeedGridView.ascx
which is essentially a GridView. It has a public property Category
which is passed in on the page using the control.
The problem I have is that I want to filter based on a dropdown list on the calling page.
Below is the codebehind for the AutomatedFeedGridView
control:
// 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
}
}
So on the first load, the GridView is bound to a List of AutomatedFeed
objects. On any subsequent calls (caused by a postback on the page containing the control) I want to run some code to filter out some of the items in the GridView. The problem is this line:
List<AutomatedFeed> x = (List<AutomatedFeed>)gvAutomatedFeedCategory.DataSource;
I've tried all of the solutions here but none of them seem to work, I always get an Object reference not set to an instance error. Am I missing something or am I doing this in completely the wrong way?
I know I could easily just make another call to Feeds.GetAutomatedFeed(Category)
but there must be a better way to do it than to make another stored procedure call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将会话中的数据源存储为
Session["x"] = x ;
,当页面回发时将其检索为
List; x = List)Session["x"];
更新:
DataSource 属性将为 null,除非您在每次回发时显式重新分配和重新绑定它。
您可以使用 Session、Cache 或 ViewState 来保留数据源。但会占用更多内存。
you can store data source in session as
Session["x"] = x ;
when page post back retrieve it back as
List<AutomatedFeed> x = List<AutomatedFeed>)Session["x"];
UPDATE:
DataSource property will be null unless you explicitly re-assign and re-bind it on every postback.
You could use Session, Cache, or ViewState to keep the DataSource. But it will take more memory.
该控件仅在 page.load 之后生成并填充,因此它不会包含任何数据。
The control is only generated and populated after the page.load, so it will not contain any data.