放假了,哎。。。一休息就是這麼多天~~~
開始努力學習了
今天練習使用Callback,在對於卻在dom上出了問題。
首先,下面這個例子是 正確的。
ClientCallbacks.aspx
<!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.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
<!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.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顯示出的錯誤是:
async
|
false |
context
|
null |
errorCallback
|
null |
xmlRequest
|
XMLHttpRequest |
eventCallback
|
OnCallback(txtUsrInfo, context) |