Session编程:从一个页面数入2个整数传到sub.aspx页中相减后输出(Button 1—click,文本框id为a,b)。
展开全部
这个功能不需要用到Session,也无需像这样滥用Session,可用通过Cross Page Post(ASP.NET 2.0)来实现。
代码如下:
Input.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Input.aspx.cs" Inherits="Input" %>
<!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>Input</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblSub1" runat="server" Text="参数1" AssociatedControlID="tbSub1"></asp:Label>
<asp:TextBox ID="tbSub1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*必须是数字" ValidationExpression="[0-9]*" ControlToValidate="tbSub1"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbSub1" ErrorMessage="*不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Label ID="lblSub2" runat="server" Text="参数2" AssociatedControlID="tbSub2"></asp:Label>
<asp:TextBox ID="tbSub2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="*必须是数字" ValidationExpression="[0-9]*" ControlToValidate="tbSub2"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbSub2" ErrorMessage="*不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button runat="server" ID="btnCrossPageSub" PostBackUrl="~/Sub.aspx" Text="提交"></asp:Button>
</div>
</div>
</form>
</body>
</html>
Input.cs
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 Input : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int Sub1
{
get { return Convert.ToInt32(tbSub1.Text); }
}
public int Sub2
{
get { return Convert.ToInt32(tbSub2.Text); }
}
}
Sub.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sub.aspx.cs" Inherits="Sub" %>
<%@ PreviousPageType VirtualPath="~/Input.aspx" %>
<!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>Sub</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Sub.cs
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 Sub : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
if (!Page.IsCrossPagePostBack)
{
int sub1 = ((Input)Page.PreviousPage).Sub1;
int sub2 = ((Input)Page.PreviousPage).Sub2;
Response.Write(String.Format("{0} - {1} = {2}", sub1, sub2, sub1 - sub2));
}
}
}
}
代码如下:
Input.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Input.aspx.cs" Inherits="Input" %>
<!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>Input</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblSub1" runat="server" Text="参数1" AssociatedControlID="tbSub1"></asp:Label>
<asp:TextBox ID="tbSub1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*必须是数字" ValidationExpression="[0-9]*" ControlToValidate="tbSub1"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbSub1" ErrorMessage="*不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Label ID="lblSub2" runat="server" Text="参数2" AssociatedControlID="tbSub2"></asp:Label>
<asp:TextBox ID="tbSub2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="*必须是数字" ValidationExpression="[0-9]*" ControlToValidate="tbSub2"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbSub2" ErrorMessage="*不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button runat="server" ID="btnCrossPageSub" PostBackUrl="~/Sub.aspx" Text="提交"></asp:Button>
</div>
</div>
</form>
</body>
</html>
Input.cs
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 Input : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int Sub1
{
get { return Convert.ToInt32(tbSub1.Text); }
}
public int Sub2
{
get { return Convert.ToInt32(tbSub2.Text); }
}
}
Sub.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sub.aspx.cs" Inherits="Sub" %>
<%@ PreviousPageType VirtualPath="~/Input.aspx" %>
<!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>Sub</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Sub.cs
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 Sub : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
if (!Page.IsCrossPagePostBack)
{
int sub1 = ((Input)Page.PreviousPage).Sub1;
int sub2 = ((Input)Page.PreviousPage).Sub2;
Response.Write(String.Format("{0} - {1} = {2}", sub1, sub2, sub1 - sub2));
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询