简体   繁体   English

如何在asp.net中手动刷新而无需更新gridview中的数据?

[英]How to update data in gridview without manual refresh in asp.net?

I have created a sharepoint webpart with a button control and gridview control. 我创建了一个带有按钮控件和gridview控件的sharepoint webpart。 When I click on the button control, a new window with two textboxes name and city will come. 当我点击按钮控件时,会出现一个带有两个文本框名称和城市的新窗口。 When I enter some data and click ok, the data is to be added to gridview. 当我输入一些数据并单击“确定”时,数据将添加到gridview中。 I have used javascript to get this window. 我用javascript来获取此窗口。

Issue:- The actual operation is performing and the data is being added to gridview. 问题: -实际操作正在执行,数据正在添加到gridview。 However, I am not able to check this new added data in gridview until I manually refresh the page. 但是,在手动刷新页面之前,我无法在gridview中检查此新添加的数据。 As this is not an ideal application behavior, can some one suggest me how we can achieve this. 由于这不是一个理想的应用程序行为,有人可以建议我如何实现这一点。

Any help will be greatly appreciated...Thank you! 任何帮助将不胜感激......谢谢!

Add the GridView and the Timer control inside an ASP.NET AJAX UpdatePanel as shown below 在ASP.NET AJAX UpdatePanel中添加GridView和Timer控件,如下所示

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
     <asp:Timer ID="Timer1" runat="server" Interval="60000" ontick="Timer1_Tick">
     </asp:Timer>

      <asp:GridView ...> 
      </asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>


protected void Timer1_Tick(object sender, EventArgs e) 
{
     GridView1.DataBind(); 
}

Interval is the number of milliseconds, the default value is 60,000 (60 seconds). Interval是毫秒数,默认值是60,000(60秒)。

A refresh is required to see the updated data. 需要刷新才能查看更新的数据。

If you want to get rid of the postback experience, you should wrap the whole thing in a update panel. 如果你想摆脱回发体验,你应该将整个事情包装在更新面板中。

To trigger the postback the asp.net-friendly way, have a button on the page; 要触发回发asp.net友好的方式,在页面上有一个按钮; hide it via css. 通过CSS隐藏它。 And through js code you can trigger the click of this button. 通过js代码,您可以触发单击此按钮。

Edit (code): 编辑(代码):

在此输入图像描述

In short this is what I am accomplishing. 简而言之,这就是我所要完成的。 I have put the Refresh button visible. 我已将“刷新”按钮显示出来。 It can be wrapped in a UpdatePanel if required. 如果需要,它可以包装在UpdatePanel中。

WebForm1.aspx: WebForm1.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.PopupAdd.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView Refresh Example</title>
    <script>
        function openPopup() {
            open('popup.aspx','_blank', 'height=300,width=200')
        }

        function refreshPage() {
            document.getElementById('btnRefresh').click();
        }
    </script>
</head>
<body>
    <h2>GridView refresh example</h2>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
        <asp:Button ID="Button1" runat="server" OnClientClick="javascript:openPopup()" Text="Add" UseSubmitBehavior="False" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Students]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs: WebForm1.aspx.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp.PopupAdd
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }
    }
}

popup.aspx: popup.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popup.aspx.cs" Inherits="WebApp.PopupAdd.popup" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function update() {
            window.opener.refreshPage();
            window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

popup.aspx.cs: popup.aspx.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp.PopupAdd
{
    public partial class popup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var sql = "insert into students (studentname) values (@name)";
            SqlConnection cxn = new SqlConnection();
            cxn.ConnectionString = ConfigurationManager
                .ConnectionStrings["ConnectionString"].ConnectionString;
            var cmd = cxn.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@name", TextBox1.Text);
            cxn.Open();
            cmd.ExecuteNonQuery();
            cxn.Close();
            ClientScript.RegisterStartupScript(this.GetType(),"update", "update()", true);
        }
    }
}

The table I used is the a simple students table with fields id, studentname. 我使用的表是一个简单的学生表,其中包含字段id,studentname。

My earlier suggestion was to hide the refresh button with css. 我之前的建议是用css隐藏刷新按钮。 ( style="visibility:hidden" ) style="visibility:hidden"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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