GridView RowDataBound在回发时不会触发 [英] GridView RowDataBound doesn't fire on postback
问题描述
在一个ASP.NET页面上,我有一个GridView填充了LINQ查询的结果。我在代码中设置DataSource,然后调用DataBind。在GridView的RowDataBound事件中,我根据查询结果选择性地隐藏某些GridView字段中的链接。 (例如,我隐藏了有问题的行的显示父项链接没有父行。)
这最初工作正常。但在回发时(当我不调用DataBind,但GridView保持通过ViewState填充时),数据显示,但RowDataBound事件(显然)不会触发,并且我的链接不会隐藏。
在回传后隐藏链接的最佳方式是什么? 我创建了一个具有只读属性的可序列化的类:PK是一个a类型的数组,行和每个链接的布尔值,指示是否启用。我们将它称为 LinkVisibility
。
LinkVisibility
实例。 b $ b LinkVisibility
(DataKeyNames在GridView中设置),然后我相应地设置链接。
我不知道这是做这件事的最好方法,但它确实有效,对于我尝试过的其他事情。
On an ASP.NET page, I have a GridView populated with the results of a LINQ query. I'm setting the DataSource in code, then calling DataBind on it. In the GridView's RowDataBound event, I'm selectively hiding links in some GridView fields based on the query results. (For instance, I hide the "Show Parent" link of the row in question has no parent row.)
This works fine initially. But on postback (when I don't call DataBind, but the GridView stays populated through ViewState), the data displays, but the RowDataBound event (obviously) doesn't fire, and my links don't get hidden.
What's the best way to get the links to be hidden after a postback?
Here's how I ended up solving this:
- I created a serializable class with readonly properties: PK of a row, and a boolean for each link indicating whether it's enabled or not. We'll call it
LinkVisibility
. - I created a serializable class inheriting from KeyedCollection to hold instances of the class above.
- I created a ViewState-backed property holding an instance of that collection.
- In my Search procedure (populating the GridView), I clear the collection.
- In RowDataBound, which initially shows/hides the links, I add a
LinkVisibility
instance to the collection for each row. - In Page.Load, when IsPostBack is true, I loop through the GridView rows. I look up the
LinkVisibility
for each one by PK in the collection (DataKeyNames is set in the GridView), and I set the links accordingly.
I don't know that this is the best way to do this, but it certainly does work, which is more than I can say for anything else I've tried.
这篇关于GridView RowDataBound在回发时不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!