自动重新加载Gridview

本文关键字:加载 Gridview 新加载 | 更新日期: 2023-09-27 18:04:42

我有一个gridview,点击按钮后出现。它从存储过程中加载,平均接收10-15条记录/5分钟,因此用户必须一次又一次地单击搜索按钮以在屏幕上加载新记录,但客户要求一旦gridview出现在屏幕上,那么它应该自动加载新记录,而无需单击按钮。有什么办法吗?

。aspx代码:

<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333"  Width="550px" OnRowDataBound="GridViewSmsComplaints_RowDataBound" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
   <Columns>
      <asp:BoundField HeaderText="ID" DataField="ID" /> 
      <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />  
      <%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" />  --%>
      <asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" /> 
      <asp:BoundField HeaderText="Message" DataField="Message" >   
         <ItemStyle Wrap="True" />
      </asp:BoundField>
      <asp:TemplateField HeaderText="IsComplaint">
         <ItemTemplate>
            <asp:CheckBox ID="ckboxIsComplaint"  runat="server" />
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView> 

cs代码:

protected void btnSearchByDate_Click(object sender, EventArgs e)
{
   try
   {
      DateTime fromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MMM/yyyy", null);
      DateTime toDate = DateTime.ParseExact(txtToDate.Text, "dd/MMM/yyyy", null);
      DataTable dt = ManageRecievedMessage.GetSmsComplaintsByDate(fromDate, toDate);
      //GridViewSmsComplaints.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
      if (dt.Rows.Count > 0)
      {
         GridViewSmsComplaints.DataSource = dt;
         GridViewSmsComplaints.DataBind();
         GridViewSmsComplaints.Visible = true;
         //gridViewComplaintsBySubject.Visible = false;
      }
      else
      {
         dt.Rows.Add(dt.NewRow());
         GridViewSmsComplaints.DataSource = dt;
         GridViewSmsComplaints.DataBind();
         int totalcolums = GridViewSmsComplaints.Rows[0].Cells.Count;
         GridViewSmsComplaints.Rows[0].Cells.Clear();
         GridViewSmsComplaints.Rows[0].Cells.Add(new TableCell());
         GridViewSmsComplaints.Rows[0].Cells[0].ColumnSpan = totalcolums;
         GridViewSmsComplaints.Rows[0].Cells[0].Text = "No Data Found for this date combination";
         GridViewSmsComplaints.Visible = true;
         //gridViewComplaintsBySubject.Visible = false;
      }
   }
   catch
   {
      // Handle Exception
   }
}

自动重新加载Gridview

GridView是一个用于显示"静态"数据的控件。据我所知,没有自动机制允许你定期更新GridView的内容。

正如@David建议的,也许你正在寻找AJAX或SignalR。它们都是可以帮助你动态获取数据的技术。

我个人的(简单的)建议是实现一个简单的WCF服务,并在你第一次点击负责填充GridView的按钮时调用它。使用轮询机制,WCF服务应该能够获取所需的数据。检索到数据后,可以更新GridView。

使用ASP怎么样?净AJAX吗?

<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
    <asp:TextBox runat="server" ID="txtFromDate" />
    <asp:TextBox runat="server" ID="txtToDate" />
    <asp:GridView runat="server" ID="GridViewSmsComplaints">
        ... Grid details ...
    </asp:GridView>
    <asp:Timer runat="server" Interval="15000" ID="RefreshSmsComplaints" OnTick="RefreshSmsComplaints_Tick" />
</asp:UpdatePanel>

背后的代码:

protected void RefreshSmsComplaints_Tick(object sender, EventArgs e)
{
   try
   {
      DateTime fromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MMM/yyyy", null);
      DateTime toDate = DateTime.ParseExact(txtToDate.Text, "dd/MMM/yyyy", null);
      DataTable dt = ManageRecievedMessage.GetSmsComplaintsByDate(fromDate, toDate);
      //GridViewSmsComplaints.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
      if(dt.Rows.Count > 0)
      {
         GridViewSmsComplaints.DataSource = dt;
         GridViewSmsComplaints.DataBind();
         GridViewSmsComplaints.Visible = true;
         //gridViewComplaintsBySubject.Visible = false;
      }
      else
      {
         dt.Rows.Add(dt.NewRow());
         GridViewSmsComplaints.DataSource = dt;
         GridViewSmsComplaints.DataBind();
         int totalcolums = GridViewSmsComplaints.Rows[0].Cells.Count;
         GridViewSmsComplaints.Rows[0].Cells.Clear();
         GridViewSmsComplaints.Rows[0].Cells.Add(new TableCell());
         GridViewSmsComplaints.Rows[0].Cells[0].ColumnSpan = totalcolums;
         GridViewSmsComplaints.Rows[0].Cells[0].Text = "No Data Found for this date combination";
         GridViewSmsComplaints.Visible = true;
         //gridViewComplaintsBySubject.Visible = false;
      }
   }
   catch
   {
      // Handle Exception
   }
}

或者你也可以使用SignalR检查,如上面提到的

嘿,试着把网格绑定函数调用到缓存回调中。你可以根据需要控制回拨。

在页面顶部定义回调,如变量声明

  private static CacheItemRemovedCallback OnCacheRemove = null;

将该方法添加到您的页面

 private void AddTask(string name, int seconds)
    {
        OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
        HttpRuntime.Cache.Insert(name, seconds, null,
            DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
            CacheItemPriority.NotRemovable, OnCacheRemove);
     }

添加调用函数并将网格绑定函数调用到回调中。

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
    {
        // do stuff here if it matches our taskname, like WebRequest
        // re-add our task so it recurs
        //DoCallyourFunction();
        AddTask(k, Convert.ToInt32(v));
    }

在页面加载时调用AddTask("DoStuff", 300);

做完所有的事情后,你的方法现在每5分钟调用一次希望能有所帮助