在我的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);
文本框值重新定义我认为..但是,它不会进入按钮点击事件..
答案 0 :(得分:0)
if (un.Equals(TextBox1.Text) && (pw.Equals(TextBox2.Text)))
{
Response.Redirect("~/homepage.aspx");
}
上面的代码需要在while循环中,else中的代码应该在while循环之后
答案 1 :(得分:0)
这就是密码文本框的工作方式,如果您需要保留该值,然后存储在Viewstate或Session中,或者执行我的操作,并在提交表单之前将用户名和密码放在最后一步。
private string Password
{
get
{
string rv = "";
if(ViewState["Password"] != null)
{
rv = ViewState["Password"].ToString();
}
return rv;
}
set
{
ViewState["Password"] = value;
}
}
//on button click
this.Password = this.PasswordTextBox.Text
答案 2 :(得分:0)
可能会帮助你
TextBox1.Attributes.Add("value", TextBox1.Text);
TextBox2.Attributes.Add("value", TextBox2.Text);
答案 3 :(得分:0)
如果不设置FormsAuthentication票证,它将无法运行。
您需要查看以下代码:http://msdn.microsoft.com/en-us/library/ka5ffkce.aspx