在javascript中使用(读取、设置)Asp.net服务器的属性、方法和事件
1、JavaScript调用服务器端属性、方法和事件
(1)HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="调用服务器端方法_Default" %>
<!DOCTYPE html PUBLIC "-//W 3C //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">
<script language=javascript>
function btnclick1()
{
var jp_test1=<%=int_sum%>;
window.alert(jp_test1);
var jp_txt1='<%=str_txt%>';
window.alert(jp_txt1);
//或window.alert('<%=str_txt%>');
var jp_pro1='<%=Session["pro"].ToString()%>';
window.alert(jp_pro1);
//或window.alert('<%=Session["pro"].ToString()%>');
var jp_price1=<%=(int)Session["price"]%>;
window.alert(jp_price1);
var intpro=<%=int_pro%>;
window.alert(intpro);
var strpro='<%=str_pro%>';
window.alert(strpro);
}
function btnclick2()
{
var jp_test2=<%=mod1()%>;
window.alert(jp_test2);
var jp_txt2='<%=gettxt1()%>';
window.alert(jp_txt2);
var jp_pro2='<%=pro1()%>';
window.alert(jp_pro2);
var jp_price2=<%=getprice1()%>;
window.alert(jp_price2);
var intpro=<%=ret_int()%>;
window.alert(intpro);
var strpro='<%=ret_str()%>';
window.alert(strpro);
}
function btnclick3()
{
<%mod2();%>
var jp_test3= <%=int_sum%>;
window.alert(jp_test3);
<%gettxt2();%>;
var jp_txt3='<%=str_txt%>';
window.alert(jp_txt3);
<%pro2();%>
var jp_pro3='<%=Session["pro"].ToString()%>';
window.alert(jp_pro3);
<%getprice2();%>
var jp_price3=<%=(int)Session["price"]%>;
window.alert(jp_price3);
<%get_int();%>
var intpro=<%=int_pro%>;
window.alert(intpro);
<%getstr();%>
var strpro='<%=str_pro%>';
window.alert(strpro);
}
function btnclick4()
{
<%Button5_Click(null,null);%>
var jp_test4= <%=int_sum%>;
window.alert(jp_test4);
var jp_txt4='<%=str_txt%>';
window.alert(jp_txt4);
var jp_pro1='<%=Session["pro"].ToString()%>';
window.alert(jp_pro1);
var jp_price1=<%=(int)Session["price"]%>;
window.alert(jp_price1);
var intpro=<%=int_pro%>;
window.alert(intpro);
var strpro='<%=str_pro%>';
window.alert(strpro);
}
function btnclick6(obj,txt,ge)
{
var jp_test6=obj.id;
window.alert(jp_test6);
var jp_test6=txt;
window.alert(jp_test6);
var jp_test6=ge;
window.alert(jp_test6);
}
</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="读取初始值" onclientclick="btnclick1()"/>
<asp:Button ID="Button2" runat="server" Text="读取返回值方法更改的值" onclientclick="btnclick2()"/>
<asp:Button ID="Button3" runat="server" Text="读取无返回值方法更改的值" onclientclick="btnclick3()"/>
<br />
<br />
<asp:Button ID="Button4" runat="server" Text="从客户端调用服务器端事件" onclientclick="btnclick4()"/>
<asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="Button5" />
<input id="Button6" type="button" value="有参数的javascript方法" onclick="btnclick6(this,this.value,null)"/><br />
<br />
<br />
<br />
</div>
<br />
</form>
</body>
</html>
(2)C#代码
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
{
public int int_sum;
public string str_txt;
public int _int_pro=100;
public string _str_pro="property";
#region 初始化测试
protected void Page_Load(object sender, EventArgs e)
{
int_sum = 123;
str_txt = "abc";
Session["pro"] = "yuwen";
Session["price"] = 333;
}
#endregion
#region 有返回值测试
protected int mod1()
{
int_sum = 321;
return int_sum;
}
protected string gettxt1()
{
str_txt = "cba";
return str_txt;
}
protected string pro1()
{
Session["pro"] = "shuxue";
return Session["pro"].ToString();
}
protected int getprice1()
{
Session["price"] = 666;
return (int)Session["price"];
}
#endregion
#region 无返回值的测试
protected void mod2()
{
int_sum = 369;
}
protected void gettxt2()
{
str_txt = "fgh";
}
protected void pro2()
{
Session["pro"] = "guanli";
}
protected void getprice2()
{
Session["price"] = 999;
}
#endregion
//事件测试
protected void Button5_Click(object sender, EventArgs e)
{
int_sum = 963;
str_txt = "ffgghh";
Session["pro"] = "dianli";
Session["price"] = 369;
int_pro = 1000;
str_pro = "propertyproperty";
}
#region 创建属性
protected int int_pro
{
get { return _int_pro; }
set { _int_pro = value; }
}
protected string str_pro
{
get { return _str_pro; }
set { _str_pro = value; }
}
#endregion
protected int ret_int()
{
int_pro = 200;
return int_pro;
}
protected string ret_str()
{
str_pro = "pro";
return str_pro;
}
protected void get_int()
{
int_pro = 300;
}
protected void getstr()
{
str_pro = "propro";
}
}
2、JavaScript更改服务器端变量值
(1)HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="JavaScript更改服务器端变量值_Default" %>
<!DOCTYPE html PUBLIC "-//W 3C //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">
<script language=javascript>
function EditSession()
{
<%Session["IntTest"]=300;%>;
window.alert(<%=(int)Session["IntTest"]%>);
<%Session["StrTest"]="JavaScript Session StrTest";%>;
window.alert('<%=Session["StrTest"].ToString()%>');
}
function EditField()
{
<%IntFilTest=1000;%>;
window.alert(<%=IntFilTest%>);
<%StrFilTest="JavaScript StrFilTest";%>
window.alert('<%=StrFilTest%>');
}
function EditProperty()
{
<%IntProTest=2000;%>;
window.alert(<%=IntProTest%>);
<%StrProTest="JavaScript StrProTest";%>;
window.alert('<%=StrProTest%>');
}
</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="更改Session变量的值" OnClientClick="EditSession()"/>
<asp:Button ID="Button2" runat="server" Text="更改字段的值" OnClientClick="EditField()"/>
<asp:Button ID="Button3" runat="server" Text="更改属性的值" OnClientClick="EditProperty()"/></div>
</form>
</body>
</html>
(2)C#代码
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 JavaScript更改服务器端变量值_Default : System.Web.UI.Page
{
protected int IntFilTest = 100;
protected string StrFilTest="StrFilTest";
protected int _IntProTest = 300;
protected String _StrProTest = "StrProTest";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["IntTest"] = 100;
Response.Write(GetIntAlert((int)Session["IntTest"]));
Session["StrTest"] = "Session StrTest";
Response.Write(GetStrAlert(Session["StrTest"].ToString()));
IntFilTest = 200;
Response.Write(GetIntAlert(IntFilTest));
StrFilTest = "StrFilTest";
Response.Write(GetStrAlert(StrFilTest));
}
}
protected int IntProTest
{
get { return _IntProTest; }
set { _IntProTest = value; }
}
protected string StrProTest
{
get { return _StrProTest; }
set { _StrProTest = value; }
}
protected string GetIntAlert(int IntMsg)
{
return "<script language=javascript>window.alert('" + IntMsg + "' );</script>";
}
protected string GetStrAlert(string StrMsg)
{
return "<script language=javascript>window.alert('" + StrMsg + "' );</script>";
}
}