在我的 login.aspx 页面中,当我单击提交按钮时,第一个文本框的值被保留。第二个文本框(密码字段)自动清除,所以我无法进入主页。即使我关闭窗口并再次打开它不工作。为什么在按钮单击中清除文本框?这是我的代码:aspx 页面:
<asp:Panel ID="Panel1" runat="server" Height="251px" Style="z-index: 100; left: 349px;
position: absolute; top: 320px" Width="415px">
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 126px; position: absolute;
top: 89px" Text="USERNAME:" ForeColor="#C04000"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 102; left: 126px; position: absolute;
top: 136px" Text="PASSWORD:" BackColor="White" BorderColor="White" ForeColor="#C04000"></asp:Label>
<asp:Label ID="Label3" runat="server" ForeColor="Red" Style="z-index: 105; left: 151px;
position: absolute; top: 166px" Text="INVALID USERNAME OR PASSWORD" Visible="False"
Width="314px"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="FIELD CANNOT BE EMPTY" Style="z-index: 106; left: 423px; position: absolute;
top: 90px" Width="227px"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="FIELD CANNOT BE EMPTY" Style="z-index: 107; left: 424px; position: absolute;
top: 135px" Width="214px"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="TextBox1"
ErrorMessage="ENTER ONLY CHARACTERS(MIN2)" Height="1px" Style="z-index: 108; left: 415px;
position: absolute; top: 88px" ValidationExpression="^[a-zA-Z\s]{2,15}$" Width="228px"></asp:RegularExpressionValidator>
<asp:Label ID="Label4" runat="server" Font-Size="XX-Large" ForeColor="#8080FF" Style="z-index: 109;
left: 204px; position: absolute; top: -62px" Text="LOGIN " Width="109px"></asp:Label>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 241px; position: absolute;
top: 89px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 242px; position: absolute;
top: 132px" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="SUBMIT" Style="z-index: 101; left: 179px; position: absolute;
top: 195px" onclick="Button1_Click"/>
</asp:Panel>
aspx.cs 页面:
protected void Page_Load(object sender, EventArgs e)
{
string pwd = TextBox2.Text;
TextBox2.Attributes.Add("value", pwd);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
String un = null;
String pw = null;
string con1 = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
OleDbConnection con = new OleDbConnection(con1);
con.Open(); //connection must be openned
OleDbCommand cmd = new OleDbCommand("SELECT * from admin", con); // creating query command
OleDbDataReader reader = cmd.ExecuteReader(); // executes query
while (reader.Read()) // if can read row from database
{
un = reader[0].ToString();
pw = reader[1].ToString();
// Get column 1 and column 3 value and print
}
if (un.Equals(TextBox1.Text) && (pw.Equals(TextBox2.Text)))
{
Response.Redirect("~/homepage.aspx");
}
else
{
Label3.Visible = true;
TextBox1.Text = "";
TextBox2.Text = "";
}
}
catch (Exception ex)
{
//MessageBox.Show("error"+ex);
}
当我在页面加载中添加它时:
string pwd = TextBox2.Text;
TextBox2.Attributes.Add("value", pwd);
我认为文本框的值已被重新定义..但是,它不会进入按钮单击事件中..