简体   繁体   中英

sorting with gridview inside a update panel in asp.net

I have a grid table, which I do data binding on page load

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

the grid view is inside a update panel and I update the panel every 3 seconds

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

the timer tick does the databinding every 3 seconds

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

and I have also enabled sorting in the table, but the sorting stays only for 3 seconds and then databinding is done and original table is restored, How can i retain the sorting even after the update to the table.

    <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">

Sorting:

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();
    }
}

When you call SortGridView() method us pass two parameters sortExpression and direction

Just declare the static variable on the page level and assign it a direction change the direction in the method

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

and sort the Dataview.

Hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM