Results 1 to 9 of 9

Thread: Webcontrols.Textbox Problem in retrieving text

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    24

    Webcontrols.Textbox Problem in retrieving text

    Hi all, I'm new to ASP.NET, can you give me a hand?

    I have a Web Form with a WebControls.Button and a WebControls.Textbox on it, and with the following code behind:

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         TextBox1.Text = "Enter Text Here"
    4.     End Sub
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Response.Write("You have entered :" & TextBox1.Text)
    8.     End Sub

    I would like to set the text on the textbox object in the Load Event, and it's up to user modification.

    After all, it should display the modifided text upon clicking the button.
    Sounds simple isn't it?

    But to my surpeise, TextBox1.text remains the initial value (i.e "Enter Text Here"), and it ignores users' modification. Can somebody advise on it?

  2. #2
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: Webcontrols.Textbox Problem in retrieving text

    Did you save your changes before running it?
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    24

    Re: Webcontrols.Textbox Problem in retrieving text

    Quote Originally Posted by simonm
    Did you save your changes before running it?
    Do you mean saving the project/aspx, etc files, before rebuilding the solution? yes i did.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    24

    Re: Webcontrols.Textbox Problem in retrieving text

    I found something related to the problem:

    If the Textbox control has NOT been assigned a value to its text property programtically (i.e textbox1.text ="something"), there won't be such a problem.

    however, when TextboxBox1.text has been set by code, the problem raised

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Webcontrols.Textbox Problem in retrieving text

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3. If Not Page.IsPostBack Then
    4.  
    5. TextBox1.Text = "Enter Text Here"
    6.  
    7. End If
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Response.Write("You have entered :" & TextBox1.Text)
    12.     End Sub

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Webcontrols.Textbox Problem in retrieving text

    The problem is you are setting the value of the TextBox on Page_Load. When the button is clicked, it causes the form to be posted again and a new Page_Load which sets the text back to the original.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    24

    Re: Webcontrols.Textbox Problem in retrieving text

    Oh, that works!

    Thanks mendhak and wey97!

  8. #8
    New Member
    Join Date
    Jan 2005
    Location
    Missouri
    Posts
    14

    Re: Webcontrols.Textbox Problem in retrieving text

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If Not Page.IsPostBack Then
    TextBox1.Text = "Enter Text Here"
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Textbox1.text = Response.Write("You have entered :" & TextBox1.Text)
    End Sub

    I know this has already been solved but i thought i would post also that you could do it this way.....this should work

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    24

    Re: Webcontrols.Textbox Problem in retrieving text

    Thanks webprog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width