在asp.net的更新面板中使用gridview进行排序

时间:2011-11-11 11:21:43

标签: c# asp.net ajax gridview ajaxcontroltoolkit

我有一个网格表,我在页面加载时进行数据绑定

if (!IsPostBack)
                {
                   BindGridViewUsers(msgID);
                }

网格视图位于更新面板内,我每隔3秒更新一次面板

  <asp:UpdatePanel ID="holder" runat="server">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                </Triggers>
                <ContentTemplate>

计时器滴答每隔3秒进行一次数据绑定

 protected void Timer_Tick(object sender, EventArgs args)
    {
        String MsgID = HttpContext.Current.Request.QueryString["MsgID"];
        int msgID = Convert.ToInt32(MsgID);
        BindGridViewUsers(msgID);
    }

我也在表中启用了排序,但排序只停留了3秒钟,然后完成数据绑定并恢复原始表格,即使更新到表格后,如何保留排序。

    <asp:BoundField DataField="TimeRead" ItemStyle-Width="25%" HeaderText="TimeRead"
                                    SortExpression="TimeRead" />
                                <asp:BoundField DataField="Name" ItemStyle-Width="45%" HeaderText="Name" SortExpression="Name" />
                                <asp:BoundField DataField="Email" ItemStyle-Width="45%" HeaderText="Email" SortExpression="Email">

排序:

private void SortGridView(string sortExpression, string direction)
{
    String MsgID = HttpContext.Current.Request.QueryString["MsgID"];
    DataTable dataTable = BindGridViewUsers(Convert.ToInt32(MsgID)).Tables[0];
    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = sortExpression + direction;
        Grid_UserTable.DataSource = dataView;
        Grid_UserTable.DataBind();
    }
}

1 个答案:

答案 0 :(得分:0)

当你调用SortGridView()方法时,我们传递两个参数sortExpression和direction

只需在页面级别声明静态变量,并为其指定方向更改方法中的方向

if (direction.Equals("ASC")) 
      direction = "DESC";
else  
      direction = "ASC";

并对Dataview进行排序。

希望这有效!