aspx獲取aspx.cs中的變量和方法


aspx.cs和aspx之間相互調用必須是同一個Web窗體。

1.aspx.cs文件

public int i = 0; 注:定義一個全局字段。

public string pub() 注:定義一個全局方法。
{
string s = "'方法'";
return s;
}

protected void Page_Load(object sender, EventArgs e)
{
for (int n = 0; n <= 100; n++)
{
i += n;
}
}

2.aspx文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
var i =<%=i%>; 注:調用aspx.cs里的i字段
var p =<%=pub()%>; 注:調用aspx.cs里的pub()方法
alert(p+" "+i);
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text=""><%=i %></asp:Label> 注:調用aspx.cs里的i字段
<asp:Label ID="Label2" runat="server" Text=""><%=pub()%></asp:Label> 注:調用aspx.cs里的pub()方法
</form>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM