GridView RowDataBound在回发时不会触发 [英] GridView RowDataBound doesn't fire on postback

查看:160
本文介绍了GridView RowDataBound在回发时不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASP.NET页面上,我有一个GridView填充了LINQ查询的结果。我在代码中设置DataSource,然后调用DataBind。在GridView的RowDataBound事件中,我根据查询结果选择性地隐藏某些GridView字段中的链接。 (例如,我隐藏了有问题的行的显示父项链接没有父行。)



这最初工作正常。但在回发时(当我不调用DataBind,但GridView保持通过ViewState填充时),数据显示,但RowDataBound事件(显然)不会触发,并且我的链接不会隐藏。



在回传后隐藏链接的最佳方式是什么? 我创建了一个具有只读属性的可序列化的类:PK是一个a类型的数组,行和每个链接的布尔值,指示是否启用。我们将它称为 LinkVisibility

  • 我创建了一个继承自KeyedCollection的可序列化类来保存上述类的实例。 >
  • 我创建了一个ViewState支持的属性来保存该集合的一个实例。
  • 在我的搜索过程(填充GridView)中,清除集合。
  • li>
  • 在RowDataBound中,最初显示/隐藏链接,我为每行的集合添加一个 LinkVisibility 实例。 b $ b
  • 在Page.Load中,当IsPostBack为true时,我遍历GridView行。我在集合中查找每个元素的 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:

    1. 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.
    2. I created a serializable class inheriting from KeyedCollection to hold instances of the class above.
    3. I created a ViewState-backed property holding an instance of that collection.
    4. In my Search procedure (populating the GridView), I clear the collection.
    5. In RowDataBound, which initially shows/hides the links, I add a LinkVisibility instance to the collection for each row.
    6. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆