Pregunta

I wonder if somebody else met the problem below with FreeTextBox like I did:

I have a FreeTextBox in my aspx page.

And the code behind to write the text in FreeTextBox (FTB) into Database:

    protected void btnWrite_Click(object sender, EventArgs e)
    {

        if(FTB.Text!="")
        {
             ...// insert FTB.Text into DataBase
             FTB.Text="";//clear the text in FTB
        }
       else LabelError.Text="Write something!";

    }

I got the problem when I do 3 steps:

  1. The first time, I let the FTB empty-> click button Write--> LabelError show: Write something! ----> the code works fine

  2. The 2nd time: I type: Hello World in FTB--> click button Write--> The FTB.Text's content is inserted into DataBase and the FTB.Text is cleared, then the Page Load again with empty FreeTextBox--> the code works fine too

  3. The 3rd time: I let the FTB empty ---> Click button Write --> the code jump into if command, The FTB.Text's content is inserted into DataBase, I check the Database, the new record is inserted with empty value ---> the code works wrong.

Try to debug in the 3rd option:

FTB.Text="";

I really dont know why even the FTB.Text="" , the code still jump inside if command and insert the FTB.Text="" into Database.

The important thing I want you all to know is: the code works fine in the first time (the FreeTextBox is empty), but it works wrong in the 3rd one (the FreeTextBox is empty after cleared by the 2nd one).

What wrong??? I wonder if there is some reason from my Chrome Browser,or Cache?

As recommended, I clear the Browser Cache, but I still got this problem.

Please help!!!

¿Fue útil?

Solución

 if(FTB.Text.ToString().Trim()!="")

or

if(!FTB.Text.ToString().Equals(""))

Otros consejos

Try to use string.IsNullOrEmpty

if(!string.IsNullOrEmpty(FTB.Text)
{
// insert value in DB
}
else
{
// show message
}

Or you can also use .Trim() as there may be some spaces

if(!string.IsNullOrEmpty(FTB.Text.Trim())
    {
    // insert value in DB
    }
    else
    {
    // show message
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top