簡體   English   中英

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

[英]sorting with gridview inside a update panel in asp.net

我有一個網格表,我在頁面加載時進行數據綁定

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

當您調用SortGridView()方法時,我們傳遞了兩個參數sortExpression和direction

只需在頁面級別上聲明靜態變量並為其指定方向即可更改方法中的方向

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

並排序Dataview。

希望這有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM