自动重新加载Gridview

时间:2014-05-23 12:31:23

标签: c# asp.net gridview

点击按钮后会出现一个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
   }
}

3 个答案:

答案 0 :(得分:0)

嘿尝试将您的网格绑定功能调用到缓存回调中。您可以根据需要控制回调。

在页面顶部定义回调,如vaiable decalartion

  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分钟调用一次 希望它有所帮助

答案 1 :(得分:0)

GridView是一个用于显示&#34;静态&#34;数据。据我所知,没有自动机制允许您定期更新GridView的内容。

正如@David建议的那样,也许您正在寻找AJAXSignalR。它们都是可以帮助您动态获取数据的技术。

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

答案 2 :(得分:0)

使用ASP.NET 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结账。