2010-03-05 120 views
2

我需要從MS SQL 2005顯示實時數據,我看到一些網友認爲建議的Ajax解決我的問題。基本上,現在我有我的Default.aspx頁面纔剛剛的解決方法,我可以能夠從我的數據庫顯示的數據。但是,一旦我手動添加數據到我的數據庫沒有更新。任何建議傢伙解決這個問題?我需要更新datagridview刷新頁面。更新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()的代碼。

謝謝你們,希望有人能幫助我在這個問題上。

回答

0

你是說你想無需任何用戶交互來更新實時頁面(就像刷新按鈕一樣),只要數據庫中的數據發生變化?

我沒有做過這樣的事情,但我想到的一種可能性是調用一個Web服務,它可以從頁面Javascript中提供最新的數據集(使用計時器重複調用Web服務),並且如果數據,導致異步回發(或使用其他函數更新數據表中的相應值)。

我不知道,但是,瀏覽器是否會處理這樣的事情順利,這取決於你擁有的數據量。