显示默认文本的文本框

时间:2013-11-21 11:36:23

标签: c# asp.net

我面临一个奇怪的问题。我在asp.net中的两个文本框显示默认文本,但是.cs文件中没有设置默认值。

以下是文本框的代码:

<tr>
    <td>
        <asp:TextBox ID="txtName" runat="server" ValidationGroup="reg" Height="32px" Width="155px"
            Style="border: 1px solid #999; background-color: #eee; width: 250px; height: 25px"
            type="text"></asp:TextBox>
            </td>
            <td>
        <asp:RequiredFieldValidator runat="server" ControlToValidate="txtName" Display="Dynamic"
            ValidationGroup="reg" ErrorMessage="Please fill your name as your Id"></asp:RequiredFieldValidator>
    </td>
</tr> 

代码背后:

    public partial class Registration : System.Web.UI.Page
    {

        MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        con.Open();
        try
        {
            if (IsUsernameEmailExist())
            {

                Messagebox("Username/Email address already exists. Please try another");
                return;
            }
            if (txtPassword.Text == txtConfirm.Text)
            {
                MySqlCommand cmd = new MySqlCommand("insert into MEMBERS (memberNAME,memberEMAIL,memberPASSWORD,memberPHONE,isACTIVE) values(@memberNAME,@memberEMAIL,@memberPASSWORD,@memberPHONE,@isACTIVE)", con);
                cmd.Parameters.AddWithValue("@memberNAME", txtName.Text);
                cmd.Parameters.AddWithValue("@memberEMAIL", txtEmail.Text);
                cmd.Parameters.AddWithValue("@memberPASSWORD", txtPassword.Text);
                cmd.Parameters.AddWithValue("@memberPHONE", txtPhone.Text);
                cmd.Parameters.AddWithValue("@isACTIVE", Label1.Text);
                cmd.ExecuteNonQuery();
                Sendemail();
                Clear();
                Messagebox("User Register Successfully .Now check your mail verification link is sent to your mail. After verification you can access your account");
                cmd.Dispose();

            }
            else
            {
                Messagebox("Passowrd Not Match");
            }
        }

        catch
        {

        }
    }
    public void Sendemail()
    {
        string ActivationUrl;
        try
        {
            MailMessage message = new MailMessage();

            message.To.Add(txtEmail.Text);
            message.Subject = "Verification Email";
            ActivationUrl = Server.HtmlEncode("http://localhost:55655/WebSite2/Verification.aspx?memberID=" + GetUserID(txtEmail.Text));
            message.Body = "<a href='" + ActivationUrl + "'>Click Here to verify your acount</a>";
            message.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new 
            smtp.EnableSsl = true;
            smtp.Send(message);
        }
        catch
        {
        }
    }
    private string GetUserID(string Email)
    {
        MySqlCommand cmd = new MySqlCommand("SELECT memberID FROM MEMBERS WHERE memberEMAIL=@memberEMAIL", con);
        cmd.Parameters.AddWithValue("@memberEMAIL", txtEmail.Text);
        string memberID = cmd.ExecuteScalar().ToString();
        return memberID;
    }

    private bool IsUsernameEmailExist()
    {
        MySqlCommand cmd = new MySqlCommand("Select * from MEMBERS where memberNAME='" + txtName.Text + "' or memberEMAIL='" + txtEmail.Text + "'", con);
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";

        Page.Controls.Add(lblMessageBox);

    }
    public void Clear()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtPassword.Text = "";
        txtConfirm.Text = "";
        txtPhone.Text = "";
    }
}

3 个答案:

答案 0 :(得分:0)

请在代码中添加以下两种组合之一

//do not change whole form tag, just add the autocomplete part
<form  autocomplete="off"> 
    //....
</form>

OR 直接将其添加到您的元素

<asp:TextBox id="yourid" runat="server" autocomplete="off">
</asp:TextBox>

答案 1 :(得分:0)

我认为在Page_Load()if(!Page.isPostBack)中调用Clear()方法可能是一个好习惯,可能会解决你的问题吗?

答案 2 :(得分:0)

if (window.getSelection)
   window.getSelection().removeAllRanges();
else if (document.selection)
   document.selection.empty();