gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:54 25 4
gpt4 key购买 nike

我需要显示来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com