1

I am facing a strange problem. Two of my textboxes in asp.net are showing the default text, but there is no default value is set not even in the .cs file.

Here is the the code for a textbox:

<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> 

Code behind:

    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 = "";
    }
}
Alberto
  • 15,626
  • 9
  • 43
  • 56

3 Answers3

0

Please add one of the following two combinations to your code

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

OR add this to your elements directly

<asp:TextBox id="yourid" runat="server" autocomplete="off">
</asp:TextBox>
Marco
  • 22,856
  • 9
  • 75
  • 124
0

I think it might just be good practice to call the Clear() method in your Page_Load() if (!Page.isPostBack) and might solve your problem?

0
if (window.getSelection)
   window.getSelection().removeAllRanges();
else if (document.selection)
   document.selection.empty();
Nunser
  • 4,512
  • 8
  • 25
  • 37
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25