前后台数据,代码交互
1.方法1,在前台用JS触发一个按钮,调用后台
<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#函数?
<script type="text/javascript">
function ClickMe() {
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span ><asp:Button ID="btn1" runat="server" Text="" onclick="btn1_Click"/></span>
<input id="Button1" type="button" value=""  onclick="ClickMe();"/>
</div>
</form>
</body>
</html>
2.方法2  用asp的格式写"<%=  %>"
<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#函数?--%>
<script type="text/javascript">
var a = "<% =SS()%>";  //注意,必须加双引号,调用后台一个SS()的方法
alert(a);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
3.方法3  表示看不懂
<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#函数? 方法三 :--%>
<script type="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
//var theForm = document.form1; //指runat=server的form
var theform;
if (window.LowerCase().indexOf("netscape") > -1) {
theform = document.forms[0];
}
else {
theform = document.forms[0];
}
if (!theform.__EVENTTARGET) {
theform.ateElement("<input type='hidden' name='__EVENTTARGET'>"));
}
if (!theform.__EVENTARGUMENT) {
theform.ateElement("<input type='hidden' name='__EVENTARGUMENT'>"));
}
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button2" runat="server" Text="服务器按钮" onclick="Button2_Click"  Visible="false"/>
<input id="Button1" name="Button1" type="button" value="方法三"  onclick="javascript:__doPostBack('Button2','')"/>
</div>
</form>
</body>
</html>
4.方法4  表示看不懂
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#函数? 方法四 :--%>
<script type="text/javascript">
function SubmitKeyClick() {javascript void 0 是什么意思
if (event.keyCode == 13) {
event.cancelBubble
= true;
document.all.FunName.value = ElementById("aaa").value;
document.forms[0].submit();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="aaa" type="text" onkeypress="SubmitKeyClick()"/>
<input type="hidden" name="FunName" />
</div>
</form>
</body>
</html>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string strFunName = Request.Form["FunName"] != null ? Request.Form["FunName"] : "";
switch (strFunName)
{
case "Print":
Print();
break;
default:
Response.Write("不到相应的方法");
break;
}
}
}
private  void Print()
{
Response.Write("这里是方法四的Print方法");
}
5.方法5:通过隐藏一个文本框来获取后台值,值得一提的是,通过这里可发现javascript脚本放在文本框后面,才能获取到后台的值,因为代码是自上而下运行的,而且由此可看出加了runat="server"时,当时便可获取后台的值
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#变量? 方法一:--%>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Hidden1" type="hidden"  runat="server"/>
<script type="text/javascript">
ElementById("Hidden1").value);
</script>
</div>
</form>
</body>
</html>
后台:
public partial class Default5 : System.Web.UI.Page
{
private string str = "方法一:通过隐藏域访问C#变量";
protected void Page_Load(object sender, EventArgs e)
{
Hidden1.Value = str;
}
}
6.方法6,直接获取后台的值
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#变量? 方法二:--%>
<script type="text/javascript">
var str="<%=str %>"; //必须加双引号
alert(str);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台:
public partial class Default6 : System.Web.UI.Page
{
public string  str="方法二:直接访问C#变量";
protected void Page_Load(object sender, EventArgs e)
{
}
}
7:方法7  后台注册JS访问C#变量
前台:
<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--1.如何在JavaScript访问C#变量? 方法三:--%>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
<script type="text/javascript">
alert(temp);
</script>
</body>
</html>
后台:public partial class Default7 : System.Web.UI.Page
{
private string temp = "方法二:后台注册JS访问C#变量";
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterStartupScript("1","<script type='text/javascript'>var temp='"+temp+"'</script>");
}
}
8.通过隐藏域
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var str = "如何在C#中访问JavaScript的变量 方法一:通过隐藏域"
function ClickMe() {
document.forms[0].submit();
}
</script>
</head>
<body>
<form id="form1"  method="post" runat="server">
<div>
<input id="Button1" type="button" value="方法一"  onclick="ClickMe();"/>
<input id="Hidden1" name="Hidden1" type="hidden" />
</div>
</form>
</body>
</html>
后台:
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Request["Hidden1"]!=null)
{
string str = Request["Hidden1"].ToString();
Response.Write(str);
}
}
}
9.在C#中访问JavaScript函数
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--  如何在C#中访问JavaScript函数? 方法一--%>
<script type="text/javascript">
function test() {
alert("在C#中访问JavaScript函数  方法一");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台:public partial class Default9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Page.RegisterStartupScript("1","<script type='text/javascript'>test();</script>");
}
}
}
10.通过Literal
前台:<html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<%--  如何在C#中访问JavaScript函数? 方法二--%>
<script type="text/javascript">
function test() {
alert("在C#中访问JavaScript函数  方法二");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="方法二" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后台:public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Literal1.Text = "<script typ
e='text/javascript'>test();</script>";
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。