c# - 在我的 asp.net 中使用 ajax 更新 datagridview 而无需刷新页面。(显示实时数据)

标签 c# asp.net ajax

我需要显示来自 MS SQL 2005 的实时数据。我看到一些博客推荐使用 Ajax 来解决我的问题。基本上,现在我的 default.aspx 页面只是为了解决问题,我可以显示我的数据库中的数据。但是一旦我手动将数据添加到我的数据库中,就没有进行任何更新。有什么建议可以解决这个问题吗?我需要在不刷新页面的情况下更新 datagridview。

这是我在 Default.aspx.cs 上的代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    protected void up1_Load(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    protected void FillDataGridView()
    {
        DataSet objDs = new DataSet();
        SqlConnection myConnection = new SqlConnection  (ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
        SqlDataAdapter myCommand;
        string select = "SELECT * FROM Categories";
        myCommand = new SqlDataAdapter(select, myConnection);
        myCommand.SelectCommand.CommandType = CommandType.Text;
        myConnection.Open();
        myCommand.Fill(objDs);

        GridView1.DataSource = objDs;
        GridView1.DataBind();
    }
}

我的 Default.aspx 上的代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Ajax Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            <Scripts>
                   <asp:ScriptReference Path="JScript.js" />      
            </Scripts>
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="up1_Load">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" Height="136px" Width="325px"/>
            </ContentTemplate>
                <Triggers> 
                    <asp:AsyncPostBackTrigger ControlID="GridView1" /> 
                </Triggers> 
        </asp:UpdatePanel>
    </form>
</body>
</html>

我现在的问题是如何调用或使用 ajax.js 以及如何编写代码以在我的 Default.aspx.cs 页面中调用 FillDataGridView()。

谢谢大家,希望有人能帮我解决这个问题。

最佳答案

您会找到一个很好的指南 here

参见代码示例 here

更新: http://hubpages.com/hub/Automatic-Refresh-data-on-page-using-AJAX-update-panel

关于c# - 在我的 asp.net 中使用 ajax 更新 datagridview 而无需刷新页面。(显示实时数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2384353/

相关文章:

javascript - 使用javascript循环遍历fileList

javascript - 使用 React.js 将富文本编辑器中的 HTML 标记保存到 MongoDB

c# - 在 C# 中以编程方式更新连接字符串后重新加载 web.config

C# ReactiveUI UserError 错误处理程序

c# - iOS webapp 启动画面缓存

asp.net - 如何在 Windows 10 上的 IIS 10 中运行 ASP.NET MVC 应用程序

php - AJAX登录无需重新加载页面

c# - C#中的工厂模型

c# - 计算整数中的有效数字

javascript - 替换特殊字符