js调用后台的方法

本文介绍了一个简单的ASP.NET示例,展示了如何通过客户端JavaScript触发服务器端C#代码来更新网页内容及获取表单输入值。具体实现包括使用UpdatePanel进行局部刷新,并通过隐藏字段传递操作指令。

<%@ 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>
function upd() {
var d = document.forms(0);
d.operation.value = "update";
d.submit();
}

function getValue() {
var d = document.forms(0);
d.operation.value = "getValue";
d.submit();
}

</script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<input type="hidden" name="operation" value="" />
<input type="button" value="update" onclick="upd()" />
<br />
<br />
begin:
<asp:TextBox ID="txt_begin" runat="server" />
<input type="button" value="getValue" onclick="getValue();" />
after:
<asp:TextBox ID="txt_end" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string operation = Request["operation"];
switch (operation)
{
case "update":
update();
break;
case "getValue":
getValue();
break;
}
}

private void getValue()
{
txt_end.Text = txt_begin.Text;
UpdatePanel1.Update();
}

private void update()
{
Response.Write("this is update method");
}
}

方法二:

1 protected string CsharpVoid( string strCC)
2 {
3strCC="你好!"+strCC;
4returnstrCC;
5}

前台JS

1 functionInit()
2 {
3 varv="中国";
4 vars=' <% = CsharpVoid( " '+v+' " ) %> ';
5 alert(s);
6 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值