C# 在我的asp.net中使用ajax更新datagridview,而不刷新页面。(显示实时数据)
我需要显示来自MS SQL 2005的实时数据。我看到一些博客推荐Ajax来解决我的问题。基本上,现在我有我的default.aspx页面,只是为了解决这个问题,我可以显示数据库中的数据。但一旦我手动将数据添加到数据库中,就不会进行更新。有没有解决这个问题的建议?我需要在不刷新页面的情况下更新datagridview 这是我在Default.aspx.cs上的代码C# 在我的asp.net中使用ajax更新datagridview,而不刷新页面。(显示实时数据),c#,asp.net,ajax,C#,Asp.net,Ajax,我需要显示来自MS SQL 2005的实时数据。我看到一些博客推荐Ajax来解决我的问题。基本上,现在我有我的default.aspx页面,只是为了解决这个问题,我可以显示数据库中的数据。但一旦我手动将数据添加到数据库中,就不会进行更新。有没有解决这个问题的建议?我需要在不刷新页面的情况下更新datagridview 这是我在Default.aspx.cs上的代码 using System; using System.Data; using System.Configuration; using
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示例
我现在的问题是如何调用或使用ajax.js,以及如何编写代码来调用Default.aspx.cs页面中的FillDataGridView()
谢谢你们,希望有人能帮我解决这个问题 你会找到一本很好的指南 参见代码示例 更新:
您是说,当数据库中的数据发生变化时,您希望在没有任何用户交互(如刷新按钮)的情况下实时更新页面吗 我还没有做过类似的事情,但我想到的一种可能性是调用一个web服务,该服务提供页面Javascript中的最新数据集(使用计时器重复调用web服务),如果数据发生更改,则导致异步回发(或使用其他函数更新数据表中的相应值)
但是,我不知道浏览器是否能够顺利处理类似的事情,这取决于您拥有的数据量。谢谢chris,我有一个示例,但它不起作用。下面是链接。