CsharpBeginner
CsharpBeginner

Reputation: 1773

Simple task of Clearing Text Fields

I am trying to clear two text fields after the data is posted. The page posts back but does not delete the text fields.

txtComment.Text = "";
txtEmail.Text = "";

How can I fix my code below.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["PicID"] != null)
        {
            int vPicID = Convert.ToInt32(Request.QueryString["PicID"]);

            BSComments DTGetComments = new BSComments();
            DataTable DTGetCommentsbyID = DTGetComments.GetCommentsByPicIDs(vPicID);


            //Create User object
            // GetMemberInfo GetMember = new GetMemberInfo();
            // DataTable DAMemberInfo = GetMember.GetmemberInfo(UserID);
            txtComment.Text = "";
            txtEmail.Text = "";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        //Check to see if the Email has an account
         BSComments CheckIFmember = new BSComments();
        DataTable DAMemberInfo = CheckIFmember.CheckIFMemberReturnNameEmail(txtEmail.Text);
        int picid =Convert.ToInt32(Request.QueryString["PicID"]);
        if (DAMemberInfo.Rows.Count > 0)
        {
            String myGuid = "";
            String MemberName = "";
            foreach (DataRow row in DAMemberInfo.Rows)
            {
                myGuid = row["Guid"].ToString();
                MemberName = row["MemberName"].ToString();

            }


            BSComments InstertComments = new BSComments();
            InstertComments.InserComment(picid, txtEmail.Text, txtComment.Text, MemberName, myGuid);
            txtComment.Text = "";
            txtEmail.Text = "";
        }
        else
        {
            txtComment.Text = "You need to have an account to post.";
        }


        txtComment.Text = "";
        txtEmail.Text = "";

    }
}

Upvotes: 0

Views: 81

Answers (1)

ChrisLively
ChrisLively

Reputation: 88064

If you have stepped through your code to determine that the txtComment and txtEmail fields have actually had their .Text values set to String.Empty, then I'd suggest the problem may not be in your code at all.

Look to see if the browser is "helpfully" prefilling those fields for you. If so, you can add an attribute in your aspx file on those controls for AutoComplete="off"

For example:

<asp:TextBox runat="server" id="txtComment" AutoComplete="off" />

Upvotes: 2

Related Questions