1

在我的 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);

我认为文本框的值已被重新定义..但是,它不会进入按钮单击事件中..

4

4 回答 4

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
于 2013-07-26T04:34:47.770 回答
0

可能这会帮助你

TextBox1.Attributes.Add("value", TextBox1.Text);
TextBox2.Attributes.Add("value", TextBox2.Text);
于 2013-07-26T04:44:52.507 回答
0
 if (un.Equals(TextBox1.Text) && (pw.Equals(TextBox2.Text)))
        {
            Response.Redirect("~/homepage.aspx");
        }

上面的代码需要在while循环里面,else里面的代码应该在while循环之后

于 2013-07-26T04:32:57.140 回答
0

如果不设置 FormsAuthentication 票证,它将无法工作。

您需要查看此代码:http: //msdn.microsoft.com/en-us/library/ka5ffkce.aspx

于 2013-07-26T04:47:04.137 回答