我已经在我的网页上添加了一个GridView到PlaceHolder,并将它的数据源设置为linq查询。 麻烦的是,当控制运行gv.DataBind(); 它会爆炸我的脸上返回一个对象没有设置bla bla bla错误。
堆栈跟踪看起来像
System.Web.UI.Web.Controls.GridView.CreateNumericPager上的System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)中的System.Web.UI.WebControls.GridView.get_StateFormatter()处理(TableRow行,PagedDataSource pagedDataSource,Boolean addFirstLastPageButtons)在System.Web.UI.WebControls.GridView.InitializePager(GridViewRow行,Int32 columnSpan,PagedDataSource pagedDataSource)....
和我的代码是
var query = from list in dc.mytable select list; gv.DataSource = query.ToList(); gv.DataBind();I've added a GridView to a PlaceHolder on my web page, and am setting it's datasource to a linq query. The trouble is that when control runs though gv.DataBind(); it'll blast on my face returning an Object not set bla bla bla error.
The stack trace looks like
at System.Web.UI.WebControls.GridView.get_StateFormatter() at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex) at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean addFirstLastPageButtons) at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource pagedDataSource) ....
and my code is
var query = from list in dc.mytable select list; gv.DataSource = query.ToList(); gv.DataBind();最满意答案
在寻找解决方案很长一段时间之后,我提出了一个可行的建议,如果您需要,我会与您分享。 不幸的是,我关闭了浏览器,不记得我在哪里看到它,以向那些解决我的问题的人提供信任。
我在这里发布,因为我没有在这里找到任何解决我的问题的答案。
这个问题的原因是,由于GridView是动态添加的,StateFormatter还没有被设置(或类似的东西),所以只需要为Load事件添加一个事件处理程序,移动gv.DataBind(); 那里:
gv.DataSource = query; gv.Load += (s, e) => { gv.DataBind(); }奇迹般有效 :)
After looking around for solutions for quite a while, I came up with a suggestion which worked, and I am sharing with you, should you need. Unfortunately I closed the browser and don't remember where I saw it, to give the credit to the guy who solved my problem.
I'm posting it here because I didn't find any answer here that solved my problem.
The reason for this problem was that since the GridView is being added dynamically, there is some issue with the StateFormatter not being set yet (or something like that), so all it takes to resolve this is adding an event handler for the Load event and moving the gv.DataBind(); there:
gv.DataSource = query; gv.Load += (s, e) => { gv.DataBind(); }Works like a charm :)
更多推荐
发布评论