好的我有一个相当简单的问题,我想定义一些会话状态变量
到目前为止,在我的代码中,在C#for aspx页面后面,我在index.aspx.cs文件中可以正常使用以下类型的声明。
Session[Constants.firstName] = "Jordan";
或
Session[Constants.firstName] = 123;
我想从index.aspx文本框中获取文本,而不是这样。
Session[Constants.firstName] = txtb_FName.Text;
问题是将此更改为变量txtb_FName.Text不起作用,当我调试它时,显示局部变量正在填充“”而不是来自我的文本框的输入。
整个CODE路径看起来像这样;
的Index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="ProjectPhase02.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Project Phase 02</title>
<link id="Link1" href="main.css" rel="stylesheet" runat="server" />
</head>
<body>
<form id="form1" runat="server" method="get" action="accountCreationConfirmation.aspx">
<div>
<asp:Label CssClass="accountFormLable" runat="server" Text="First Name: "></asp:Label>
<asp:TextBox ID="txtb_FName" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Last Name: "></asp:Label>
<asp:TextBox ID="txtb_LName" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="txtb_UName" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Password: "></asp:Label>
<asp:TextBox ID="txtb_Password" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Address: "></asp:Label>
<asp:TextBox ID="txtb_Address" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Email: "></asp:Label>
<asp:TextBox ID="txtb_Email" runat="server"></asp:TextBox>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Phone: "></asp:Label>
<asp:TextBox ID="txtb_Phone" runat="server"></asp:TextBox>
<br />
<asp:Button ID="submitButton" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectPhase02
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session[Constants.firstName] = txtb_FName.Text;
Session[Constants.lastName] = txtb_LName.Text;
Session[Constants.userName] = txtb_UName.Text;
Session[Constants.password] = txtb_Password.Text;
Session[Constants.address] = txtb_Address.Text;
Session[Constants.email] = txtb_Email.Text;
Session[Constants.phoneNumber] = txtb_Phone;
}
}
}
Constants.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectPhase02
{
public static class Constants
{
public static string firstName = "thatFName";
public static string lastName = "thatLName";
public static string userName = "thatUName";
public static string password = "thatPass";
public static string address = "thatAddress";
public static string email = "thatEmail";
public static string phoneNumber = "thatPNumber";
}
}
accountCreationConfirmation.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectPhase02
{
public partial class accountCreationConfirmation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string fN = (string) Session[Constants.firstName];
string lN = (string) Session[Constants.lastName];
string uN = (string) Session[Constants.userName];
string pW = (string) Session[Constants.password];
string ad = (string) Session[Constants.address];
string em = (string) Session[Constants.email];
string pN = (string) Session[Constants.phoneNumber];
lbl_FName.Text = fN;
lbl_LName.Text = lN;
lbl_UName.Text = uN;
lbl_Password.Text = pW;
lbl_Address.Text = ad;
lbl_Email.Text = em;
lbl_Phone.Text = pN;
}
}
}
accountCreationConfirmation.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="accountCreationConfirmation.aspx.cs" Inherits="ProjectPhase02.accountCreationConfirmation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Project Phase 02</title>
<link id="Link1" href="main.css" rel="stylesheet" runat="server" />
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:Label CssClass="accountFormLable" runat="server" Text="First Name: "></asp:Label>
<asp:Label ID="lbl_FName" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Last Name: "></asp:Label>
<asp:Label ID="lbl_LName" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="User Name: "></asp:Label>
<asp:Label ID="lbl_UName" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Password: "></asp:Label>
<asp:Label ID="lbl_Password" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Address: "></asp:Label>
<asp:Label ID="lbl_Address" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Email: "></asp:Label>
<asp:Label ID="lbl_Email" runat="server"></asp:Label>
<br />
<asp:Label CssClass="accountFormLable" runat="server" Text="Phone: "></asp:Label>
<asp:Label ID="lbl_Phone" runat="server"></asp:Label>
<br />
</div>
</form>
</body>
</html>
的main.css
.accountFormLable {
width: 100px;
float: left;
display: block;
clear: left;
}