今天遇到個很怪的問題。。。js dom的,瀏覽器作怪

放假了,哎。。。一休息就是這麼多天~~~

開始努力學習了

今天練習使用Callback,在對於卻在dom上出了問題。

首先,下面這個例子是 正確的。

ClientCallbacks.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallbacks.aspx.cs" Inherits="ClientCallbacks" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>無標題頁</title>
    
<script language="javascript">
    
function DoSearch()
    
{
        
var txtFirstName=document.getElementById("txtUsrName");
        CallServer(txtFirstName.value,
"");
    }

    
function ReceiveServerData(txtUsrInfo)
    
{
        Results.innerText
=txtUsrInfo;
    }

    setInterval(
'DoSearch()',1000);
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        姓名:
<input id="txtUsrName" style="position: static" type="text" />
    
<br />
    
<span id="Results" style="background-color:Pink; width:500px;"></span>
    
</div>
    
</form>
</body>
</html>

ClientCallbacks.aspx.cs

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ClientCallbacks : System.Web.UI.Page,ICallbackEventHandler
{
    
protected string txtUsrInfo;

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string cbReference = Page.ClientScript.GetCallbackEventReference(this"arg""ReceiveServerData""context");
        
string callbackScript = "function CallServer(arg,context)" + "" + cbReference + " };";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "CallServer", callbackScript, true);
    }


    
//trigger callback event
    public void RaiseCallbackEvent(string txtFirstName)
    
{
        
if (txtFirstName != null)
        
{
            SqlConnection conn 
= new SqlConnection("data source=./SQLEXPRESS;initial catalog=Northwind;user id=sa;password=mssqlfs");
            conn.Open();

            SqlCommand cmd 
= new SqlCommand("select EmployeeID,FirstName,City,Address from Employees where FirstName=@FirstName", conn);
            cmd.Parameters.Add(
"@FirstName", SqlDbType.NVarChar, 10).Value = txtFirstName;
            SqlDataReader dr 
= cmd.ExecuteReader();

            
if (dr.Read())
            
{
                txtUsrInfo 
= "員工代號:" + dr["EmployeeID"+ " ," +
                    
"姓名:" + dr["FirstName"+ " ," +
                    
"居住地址:" + dr["City"+ " ," +
                    
"地址:" + dr["Address"].ToString().Replace(" """+ " ," +
                    
"服務器查詢時間:" + DateTime.Now.ToLongTimeString();

            }

            
else
            
{
                
if (String.IsNullOrEmpty(txtFirstName))
                
{
                    txtUsrInfo 
= "請輸入姓名";
                }

                
else
                
{
                    txtUsrInfo 
= "查無此人!";
                }


            }

            cmd.Dispose();
            dr.Dispose();
            conn.Dispose();
        }

    }


    
public string GetCallbackResult()
    
{
        
return txtUsrInfo;
    }

}

 

------------------------------------------------------------------------------------------------------------------

然後,下面這個卻出了小問題。

ClientCallbackSimple.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallbacksSimple.aspx.cs" Inherits="ClientCallbacksSimple" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>cccc</title>
    
<script type="text/javascript">
    
function OnCallback(txtUsrInfo,context)
    
{
       alert(txtUsrInfo);
       Results.innerText
=txtUsrInfo;
    }

    
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        name:
<input id="txtUsrName" style="position: static" type="text" />
        
<input id="btnCallback" style="position: static" type="button" value="button" onclick="<%=ClientScript.GetCallbackEventReference(this,"document.form1.txtUsrName.value","OnCallback",null) %>" /><br />
        
<div id="Results"></div>
    
</div>
    
</form>
</body>
</html>

ClientCallbackSimple.aspx.cs

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ClientCallbacksSimple : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
    
protected string txtUsrInfo;

    
protected void Page_Load(object sender, EventArgs e)
    
{
    }

    
//trigger callback event
    public void RaiseCallbackEvent(string txtFirstName)
    
{
        
if (txtFirstName != null)
        
{
            SqlConnection conn 
= new SqlConnection("data source=./SQLEXPRESS;initial catalog=Northwind;user id=sa;password=mssqlfs");
            conn.Open();

            SqlCommand cmd 
= new SqlCommand("select EmployeeID,FirstName,City,Address from Employees where FirstName=@FirstName", conn);
            cmd.Parameters.Add(
"@FirstName", SqlDbType.NVarChar, 10).Value = txtFirstName;
            SqlDataReader dr 
= cmd.ExecuteReader();

            
if (dr.Read())
            
{
                txtUsrInfo 
= "員工代號:" + dr["EmployeeID"+ " ," +
                    
"姓名:" + dr["FirstName"+ " ," +
                    
"居住地址:" + dr["City"+ " ," +
                    
"地址:" + dr["Address"].ToString().Replace(" """+ " ," +
                    
"服務器查詢時間:" + DateTime.Now.ToLongTimeString();

            }

            
else
            
{
                
if (String.IsNullOrEmpty(txtFirstName))
                
{
                    txtUsrInfo 
= "請輸入姓名";
                }

                
else
                
{
                    txtUsrInfo 
= "查無此人!";
                }


            }

            cmd.Dispose();
            dr.Dispose();
            conn.Dispose();
        }

    }


    
public string GetCallbackResult()
    
{
        
return txtUsrInfo;
    }

}

用firebug顯示出的錯誤是:

 

 

 

Results is not defined
OnCallback("請輸入姓名", null)ClientCallbacksSi... (line 13)
WebForm_CallbackComplete()WebResource.axd (line 162)
 Results.innerText=txtUsrInfo;
ClientCallbacksSi... (line 13)
   
async
false
context
null
errorCallback
null
xmlRequest
XMLHttpRequest
eventCallback
OnCallback(txtUsrInfo, context)
但是我定義了啊。。。而且aspx文件和第一個正常顯示的網頁(ClientCallbacks.aspx)幾乎一樣,關於
Results.innerText語句並沒有變化。
問題解決:
我給VS設的默認瀏覽器是FIREFOX,突然我想會不會是瀏覽器的問題,
我用IE打開後,果然,正確顯示。。。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章