补充关于前后台调用的问题

bsetsail 2009-05-27 04:53:07
在takako_mu 的基礎上補充我自己知道的另外兩種方法
http://topic.csdn.net/u/20090519/09/c14682c9-b3ec-45f7-a1bb-86872e28f942.html是第一種

第二種:利用ICallbackEventHandler
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseICallbackEventHandler.aspx.cs" Inherits="UseICallbackEventHandler" %>
<!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>
<%--
該類繼承自 System.Web.UI.ICallbackEventHandler
定義一個公用變量 rotected string returnValue;
Server呼叫RaiseCallbackEvent方法進行事件處理
GetCallbackResult方法將結果返回到Client
--%>
<script type="text/ecmascript">
function PostValue()
{
CallServer(document.getElementById("TextBox1").value, "");
}
function ReceiveServerData(rValue)
{
document.getElementById("TextBox2").value = rValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<input type="button" onclick="PostValue();" value="......" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class UseICallbackEventHandler : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected string returnValue;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
string callbackScript;
callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}
public void RaiseCallbackEvent(string eventArgument)
{
returnValue = "Hello: " + eventArgument;
}
public string GetCallbackResult()
{
return returnValue;
}
}
...全文
99 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
purple_yuanling 2010-07-09
  • 打赏
  • 举报
回复
收藏以下
amandag 2009-05-27
  • 打赏
  • 举报
回复
这两种方法都不怎么用了
mengxj85 2009-05-27
  • 打赏
  • 举报
回复
Up,支持一下,在05上以前还经常用第二种法……
skyaspnet 2009-05-27
  • 打赏
  • 举报
回复
学习
jlj84237485 2009-05-27
  • 打赏
  • 举报
回复
帮顶了
阿非 2009-05-27
  • 打赏
  • 举报
回复
ajax 当你真正熟悉了这4个字母的含义时,会写出比现在更符合题意的解决方案的。
kfps8kfps8 2009-05-27
  • 打赏
  • 举报
回复
支持下
bsetsail 2009-05-27
  • 打赏
  • 举报
回复
是啊
用的是VS2008自帶的ajax
zyug 2009-05-27
  • 打赏
  • 举报
回复
呵呵,我想你借助了很多工具,然后按工具分类之
其实.你应该更注意更多一层

js ->post/get url request->server ->response ->解析,显示

而你上面的解决方法均封装了这些步骤


每个封装你称之为一个方法.呵呵.
bsetsail 2009-05-27
  • 打赏
  • 举报
回复
第三種:利用UpdatePanel
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseUpdatePanel.aspx.cs" Inherits="UseUpdatePanel" EnableEventValidation="false" %>
<!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>
<%--
在用Button2調用Button1的事件的時候在Page中加上 EnableEventValidation="false"
利用UpdatePanel實現頁面局部刷新
--%>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button1" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<input type="button" id="Button2" name="Button2" onclick="Fun()" value="Do Button1" />
</div>
</form>
<script language="javascript" type="text/javascript">
function Fun()
{
__doPostBack(form1.Button1.id, form1.Button1.value);
}
</script>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UseUpdatePanel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.TextBox2.Text = this.TextBox1.Text;
}
}

62,249

社区成员

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

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

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

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