利用js 调用后台写的方法
1 <script src="js/jquery-1.7.1.min.js"></script> 2 2 <script> 3 3 $(function () { 4 4 $(".btn").click(function () { 5 5 $("#txtname").val(‘<%=Session["Name"]%>‘); 6 6 $("#txtdate").val(‘<%=GetDate()%>‘); 7 7 $("#txtnum").val(‘<%=GetNum()%>‘); 8 8 }); 9 9 }); 10 10 </script> 11 11 12 12 13 13 14 14 15 15 <body> 16 16 <form id="form1" runat="server"> 17 17 <div> 18 18 <asp:TextBox ID="txtname" runat="server"></asp:TextBox><br /> 19 19 <asp:TextBox ID="txtdate" runat="server"></asp:TextBox><br /> 20 20 <asp:TextBox ID="txtnum" runat="server"></asp:TextBox> 21 21 <input type="button" class="btn" value="提交" /> 22 22 </div> 23 23 </form> 24 24 </body>
前台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 namespace 测试 9 { 10 public partial class 前台调用后台方法 : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 Session["Name"] = "黎明"; 15 } 16 public static string GetNum() 17 { 18 string num = "张三"; 19 return num; 20 } 21 public string GetDate() 22 { 23 string date = System.DateTime.Now.ToString("yyyy-MM-dd"); 24 return date; 25 } 26 } 27 }
后台代码
时间: 2024-10-18 04:53:06