一个简单页面回调的代码,照书抄的,但怎么也运行不了,错在哪里呢?

zhangns 2008-11-13 03:14:11
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 type="text/javascript">
function GetNumber()
{
UseCallback();
alert("aa");
}

function GetRandomNumberFromServer(TextBox1,context)
{
document.forms[0].TextBox1.value=TextBox1;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" onclick="GetNumber()" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>

</body>
</html>

后台页面:
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;

public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string _callbackResult = null;

protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetRandomNumberFromServer", "context");
string cbScript = "function UseCallback(arg,context)" + "{" + cbReference + ";" + "}";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbReference, true);
}

public void RaiseCallbackEvent(string eventArg)
{
Random rnd = new Random();
_callbackResult = rnd.Next().ToString();
}

public string GetCallbackResult()
{
return _callbackResult;
}
}

运行时报错:
行:53
Char:1
错误 :"arg"未定义
代码:0
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiezi316 2008-11-13
  • 打赏
  • 举报
回复
用ajax.net pro吧,比这个用起来方便
andrewsway 2008-11-13
  • 打赏
  • 举报
回复

<script language="javascript" type="text/javascript">
<!--
function CallServer(inputcontrol, context) {
context.innerHTML = '<IMG SRC="images/001.gif" />';
arg = inputcontrol.value;
<%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context")%>;
}
function ReceiveServerData(result, context) {
context.innerHTML = result;
}
//-->
</script>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="CallServer(TextBox1, Label1);" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


string ICallbackEventHandler.GetCallbackResult()
{
// throw new Exception("The method or operation is not implemented.");
System.Threading.Thread.Sleep(1000);
return "输入字符串:" + result;
}

void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
// throw new Exception("The method or operation is not implemented.");
result = eventArgument;
}

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧