今天遇到个很怪的问题。。。js dom的,浏览器作怪
2007-08-04 12:23
239 查看
放假了,哎。。。一休息就是这么多天~~~
开始努力学习了
今天练习使用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显示出的错误是:
POST http://localhost/SutraWay/ClientCallbacksSimple.aspx (5140ms)WebResource.axd (line 87)
Results is not defined
OnCallback("请输入姓名", null)ClientCallbacksSi... (line 13)
WebForm_ExecuteCallback(Object async=false xmlRequest=XMLHttpRequest)WebResource.axd (line 204)
WebForm_CallbackComplete()WebResource.axd (line 162)
Results.innerText=txtUsrInfo;
ClientCallbacksSi... (line 13)
但是我定义了啊。。。而且aspx文件和第一个正常显示的网页(ClientCallbacks.aspx)几乎一样,关于
Results.innerText语句并没有变化。
问题解决:
我给VS设的默认浏览器是FIREFOX,突然我想会不会是浏览器的问题,
我用IE打开后,果然,正确显示。。。
开始努力学习了
今天练习使用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显示出的错误是:
POST http://localhost/SutraWay/ClientCallbacksSimple.aspx (5140ms)WebResource.axd (line 87)
Results is not defined
OnCallback("请输入姓名", null)ClientCallbacksSi... (line 13)
WebForm_ExecuteCallback(Object async=false xmlRequest=XMLHttpRequest)WebResource.axd (line 204)
WebForm_CallbackComplete()WebResource.axd (line 162)
Results.innerText=txtUsrInfo;
ClientCallbacksSi... (line 13)
async | false |
context | null |
errorCallback | null |
xmlRequest | XMLHttpRequest |
eventCallback | OnCallback(txtUsrInfo, context) |
Results.innerText语句并没有变化。
问题解决:
我给VS设的默认浏览器是FIREFOX,突然我想会不会是浏览器的问题,
我用IE打开后,果然,正确显示。。。
相关文章推荐
- 今天遇到的一个浏览器会记录密码缓存的问题
- 今天遇到的各个浏览器兼容问题
- 今天遇到Oracle审计表AUD$数据过大问题
- 今天遇到了Cookie的问题,网上搜来些资料,记录一下备忘!
- OGRE今天遇到的问题
- 今天遇到了一个有趣的问题是突然运行vs2005网页打不开了
- 今天遇到的问题 _ 关于asp错误处理
- 今天更换主键时遇到的一个问题
- 今天遇到的一个比较变态的问题
- 今天遇到的一个问题----java String.split( Regex )的误区
- 今天遇到的一个奇葩的NoClassFound的问题
- 自定义TWebBrowser浏览器控制遇到的一些问题
- 被流氓360设置浏览器主页的解决办法(如果你也遇到了跟我一样的问题,不妨看一下是不是这个原因)
- 今天在写程序中遇到的问题与大家分享一下
- xib今天遇到的各种问题
- 今天在学习C语言循环时遇到的问题
- 今天遇到的一个IIS 下treeview不能显示的问题
- 今天遇到一个问题,原来在html的form中如果只有一个type="text"的控件,如果在text中点击回车键,就是自动提交表单!
- 今天遇到了一个很有趣的问题
- 今天遇到的三个问题