gitesh tyagi
gitesh tyagi

Reputation: 91

textbox.text always returning empty string instead of user's entered text

I am very surprised to see last night my code was working fine and the next day suddenly my textbox.text always have empty string..
My code is:

 Name of Event* :

    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />

Code behind:

protected void Page_Load(object sender, EventArgs e) {
} 

protected void create_Click(object sender, EventArgs e) {

    if (!object.Equals(Session["UserLoginId"], null)) { 

        int mid = 0; 
        int cid = 0; 
        bool valid = true;

        if (this.TextBox1.Text == "") {

            error.Text = "<p style='color:red'>Marked Fields are compulsory!!</p>"; 

        }

        else { 
            .... // database insert .... 
        }

I am always ending up with an error.text value.

Why?

Upvotes: 9

Views: 18397

Answers (5)

Daniel D.
Daniel D.

Reputation: 21

I had a similar problem. I had a textarea feeding data to a database on postback. I also designed the page to populate the textarea from the same database field. The reason it failed was because I forgot to put my reload logic inside an if(!IsPostPack) {} block. When a post back occurs the page load event gets fired again and my reload logic blanked the textarea before I could record the initial value.

Upvotes: 2

Sophie
Sophie

Reputation: 324

I am having this issue, but i'm using Telerik Ajax request with target in javascript. I have a RadTextBox and a RadButton. I call the same code from both of them but if i call it from the textBox and request a postback, the textbox is empty in the page load. So i call RadAjaxManager.ajaxRequestWithTarget('someUserControlClientID', 'myTextBoxText'); So in the page_load, i can grab the text, in case it's not sent to the server with the arguments sent, Request.Form("__EVENTARGUMENT") will hold my textBox value. by the way, RadAjaxManager.ajaxRequestWithTarget is like __doPostBack('targetID', 'arguments');. Hope this helps someone. I don't have time to investigate why i lose the text with this request, so this is the workaround i did.

Upvotes: 0

user1131039
user1131039

Reputation: 51

Had a similar problem with text boxes getting cleared on adding a new row. It was the issue of the page reloading when the add button was clicked.

Fixed the issue by adding:

Sub Page_Load
  If Not IsPostBack Then 
    BindGrid()
  End If
End Sub

Per Microsoft's documentation http://msdn.microsoft.com/en-us/library/aa478966.aspx.

Upvotes: 5

Anyname Donotcare
Anyname Donotcare

Reputation: 11393

If the page make a post back, then all the data , the user entered ,will be erased,as the controls are stateless, so u should keep your data entry through EnableViewState = true.

Upvotes: 1

darren
darren

Reputation: 19399

Kinda mentioned but you should make sure your checking your that Post_Back event is not clearing your textbox. It would by default. Try something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        {
            if (this.TextBox1.Text == "")
            {
                error.Text = "<p style='color:red'>Marked Fields are compulsory!!</p>";
            }
        else
        {
            //.....
        }
    }
}

Upvotes: 3

Related Questions