I need to display a real time data from MS SQL 2005. I saw some blogs that recommend Ajax to solve my problem. Basically, right now I have my default.aspx page only just for a workaround I could able to display the data from my DB. But once I add data manually to my DB there's no updating made. Any suggestions guys to fix this problem? I need to update datagridview with out refreshing the page.
Here's my code on 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();
}
}
Code on my 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:ScriptManage开发者_Go百科r>
<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>
My problem now is how to call or use the ajax.js and how to write a code to call the FillDataGridView() in my Default.aspx.cs page.
Thank you guys, hope anyone can help me on this problem.
You will find a good guide for this here
See code examples here
Update: http://hubpages.com/hub/Automatic-Refresh-data-on-page-using-AJAX-update-panel
Are you saying that you want to update the page in realtime without any user interaction (like a refresh button) whenever the data in your DB changes?
I havent done anything like this, but one possibility that comes to my mind is calling a web service that gives the latest dataset from your page Javascript (using timer to repeatedly call the webservice) and if there is a change in the data, cause a asynchronous postback (or use another function to update the corresponding values in the datatable).
I dont know, however, whether the browser will handle something like that smoothly, depending on the amount of data you have.
精彩评论