Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My textbox ignores input.

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
My textbox's .text value never changes, and its textChanged event never fires. Here are the details:

1. It is a System.Web.UI.WebControls.Textbox. It is NOT an html textbox.
2. The web application is written in asp.net 2.0 in vb not c#.
3. The codebehind tag is:
Code:
<asp:TextBox ID="firstNameTextBox" onTextChanged="firstNameTextBox_TextChanged" runat="server" ></asp:TextBox>
4. The handler is:
Code:
Protected Sub firstNameTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstNameTextBox.TextChanged
        Dim x As String = firstNameTextBox.Text
    End Sub
5. But that handler never fires.
6. Here are some important property settings:
autopostback is false
causes validation is false
enableviewstate is true
enabled is true
8. You CAN type in the textbox and the letters seem to change.
9. Please help me!

 
1. when you use onTextChanged of a textbox and want to run code after the textbox value changes (the focus must be lost and the value changed for the event to fire) then you should have AutoPostBack="true" also in the html of your asp:TextBox tag.

2. If you dont want the postback to occur until later, during another event, you can, but must be sure you are not setting the value overriding it during that postback.

3. Be sure in the page_load event you use If Not Page.IsPostBack Then .... bla bla

 
Many people are confused about the TextChanged event of a textbox. It does not fire for each keystroke as you expect. And as adamroof has explained, a postback needs to occur for that event to fire. If you want something to happen on each keystroke, you need to use clien-side code such as javascript's OnKeyUp event, OnChange will only fire when focus is lost.
 
Let me explain further. I want the user to enter information into several text boxes. Then they will press a button to complete the operation. Unfortunately when they press the button, nothing happens. I only dug into the textchanged event in an attempt to learn more about my problem.

The obvious question is- does the button work at all? It does work because when I add a response.write to the button's handler the button performs the response.write when clicked. I can also write code referencing the text box in the button's handler, and I don't get an error. But (during debugging) when I mouseover mytextbox.text in the button's handler, it shows the textbox's initial value not the user's input.
 
I added a new button and textbox to the form. If I access the new textbox's value from the new button's handler, it works properly. But IF I try to get the old textbox's value from the new button's handler, that does not work.
 
I have determined why my page behaved unpredictably. Normally you enclose most of the Page_Load code in “if ispostback=false”. Two days ago I intentionally omitted that structure on this page. Unfortunately, the next day I forgot about my unorthodox decision. So each time I pressed the button, the computer retrieved the original values from the database before it started on the button’s handler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top