【问题标题】:DataGrid in ASP .NET conflict of interest with Other postback eventsASP .NET 中的 DataGrid 与其他回发事件的利益冲突
【发布时间】:2012-06-12 11:10:22
【问题描述】:

我有一个绑定到通用集合的数据网格。在 Page_Load 事件中,我检查 !this.IsPostback 并相应地调用网格上的 DataBind。

然后,如果我尝试通过指定唯一名称和排序表达式来实现排序,它希望我调用 DataBind,即使页面是回发。

这种情况通常如何处理?在 Page_Load 中调用无条件 DataBind 似乎不是一个好主意。

【问题讨论】:

    标签: c# asp.net data-binding datagrid postback


    【解决方案1】:

    在 SortCommand 事件中调用您的数据绑定代码:

    void DataGrid1_SortCommand(Object sender, DataGridSortCommandEventArgs e)
        {
            // Retrieve the data source.
            DataTable dt = YOURDATA;
    
            // Create a DataView from the DataTable.
            DataView dv = new DataView(dt);
    
            // The DataView provides an easy way to sort. Simply set the
            // Sort property with the name of the field to sort by.
            dv.Sort = e.SortExpression;
    
            // Rebind the data source and specify that it should be sorted
            // by the field specified in the SortExpression property.
            DataGrid1.DataSource = dv;
            DataGrid1.DataBind();
        }
    

    【讨论】:

    • 谢谢。我假设这应该处理由于 IsPostBack 而在 Page_Load 中错过的 DataBind 事件,对吗?
    • 是的,它会在排序时处理您的数据网格数据数据绑定。
    【解决方案2】:

    如果您更改排序,则始终需要重新加载控件,因为控件需要重新填充而不是更改。自从我完成任何 asp.net 以来已经有一段时间了,但我很确定无论您是否调用 databind,数据都绑定在每个回帖上,而您不必手动执行此操作的唯一原因是视图状态.

    【讨论】:

      猜你喜欢
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2020-05-18
      • 2010-11-12
      相关资源
      最近更新 更多