放假了,哎。。。一休息就是这么多天~~~
开始努力学习了
今天练习使用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显示出的错误是:
但是我定义了啊。。。而且aspx文件和第一个正常显示的网页(ClientCallbacks.aspx)几乎一样,关于
Results.innerText语句并没有变化。
问题解决:
我给VS设的默认浏览器是FIREFOX,突然我想会不会是浏览器的问题,
我用IE打开后,果然,正确显示。。。