第二种方法:在上面的方法中我们必须要在前台绑定后台,那么如果不绑定呢?我们这样做:
直接把GetCallbackEventReference当做js函数中的一个实现内容,然后把这个js函数注册到客户端。
前台TestPage代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function test()
{
var lb = document.getElementById("Select1");
//取的那个下拉框
var con = lb.options[lb.selectedIndex].text;
//得到你选择的下拉框的文本再调用呢个CallTheServer,是一个由服务器端输出的js函数
CallTheServer(con,’’);
}
function ReceiveServerData(rValue)
{
Results.innerHTML = rValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<option value=1 selected="selected">老鼠徒弟</option>
<option value=2>吴旗娃师傅</option>
</select>
<br />
<br />
<input οnclick="test()" value="从服务器返回下拉框文本" type=button>
<br />
<br />
<span ID="Results"></span>
<br />
</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 TestPage : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallTheServer(arg,context)" +"{ " + cbReference + "} ;";
Page.ClientScript.RegisterStartupScript(this.GetType(),"abcdefg",callbackScript, true);
//第四个参数代表是不是要自动给着脚本加上<script type="text/javascript"></script>标记,当然要加啊
}
public String RaiseCallbackEvent(String eventArgument)
{
return "你选择的是" + eventArgument;
}
}
下面是执行结果:
本文转自
http://www.builder.com.cn/2007/1107/614445.shtml
直接把GetCallbackEventReference当做js函数中的一个实现内容,然后把这个js函数注册到客户端。
前台TestPage代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function test()
{
var lb = document.getElementById("Select1");
//取的那个下拉框
var con = lb.options[lb.selectedIndex].text;
//得到你选择的下拉框的文本再调用呢个CallTheServer,是一个由服务器端输出的js函数
CallTheServer(con,’’);
}
function ReceiveServerData(rValue)
{
Results.innerHTML = rValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<option value=1 selected="selected">老鼠徒弟</option>
<option value=2>吴旗娃师傅</option>
</select>
<br />
<br />
<input οnclick="test()" value="从服务器返回下拉框文本" type=button>
<br />
<br />
<span ID="Results"></span>
<br />
</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 TestPage : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallTheServer(arg,context)" +"{ " + cbReference + "} ;";
Page.ClientScript.RegisterStartupScript(this.GetType(),"abcdefg",callbackScript, true);
//第四个参数代表是不是要自动给着脚本加上<script type="text/javascript"></script>标记,当然要加啊
}
public String RaiseCallbackEvent(String eventArgument)
{
return "你选择的是" + eventArgument;
}
}
下面是执行结果:
本文转自
http://www.builder.com.cn/2007/1107/614445.shtml