繁体   English   中英

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

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

我创建了一个带有按钮控件和gridview控件的sharepoint webpart。 当我点击按钮控件时,会出现一个带有两个文本框名称和城市的新窗口。 当我输入一些数据并单击“确定”时,数据将添加到gridview中。 我用javascript来获取此窗口。

问题: -实际操作正在执行,数据正在添加到gridview。 但是,在手动刷新页面之前,我无法在gridview中检查此新添加的数据。 由于这不是一个理想的应用程序行为,有人可以建议我如何实现这一点。

任何帮助将不胜感激......谢谢!

在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是毫秒数,默认值是60,000(60秒)。

需要刷新才能查看更新的数据。

如果你想摆脱回发体验,你应该将整个事情包装在更新面板中。

要触发回发asp.net友好的方式,在页面上有一个按钮; 通过CSS隐藏它。 通过js代码,您可以触发单击此按钮。

编辑(代码):

在此输入图像描述

简而言之,这就是我所要完成的。 我已将“刷新”按钮显示出来。 如果需要,它可以包装在UpdatePanel中。

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中:

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:

<%@ 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:

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

我使用的表是一个简单的学生表,其中包含字段id,studentname。

我之前的建议是用css隐藏刷新按钮。 style="visibility:hidden"

暂无
暂无

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

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