Monday, March 26, 2012

Trouble posting to Database from Drop Down List and Text box

I'm having real trouble posting from the webpage back to a database. This is supposed to be one of the easiest things to do in ASP.NET. I've checked viewstate on all of the controls and at the page level and all are set to true.

When I type in some text information, no other characters but plane text, and hit the submit button the text disappears from the box, the drop downlist returns to default, the subroutine runs and an empty space is submitted to the database.

I've verified that the procedure in my namespace is running correctly by allowing the subroutine in the code behind to trigger the SPROC in the namespace. The 'hardcoded' values are inserted into the database.

I also placed a label.text = textbox.text into the subroutine and it is always set to a blank. Even on page intialization. None the less, I wanted to see if running the subroutine would place text inside of the label and it didn't. If I hard code the label, such as label.text = "stanley" then stanley appears when you click the link button. Thus, I'm sure that the subroutine is running, its appears that the text in the text box is being lost when the page makes a round trip.

Any ideas?

SethLet's see your code.
Be sure that you are wrapping any code that initializes the values textbox and dropdownlist in your Page_Load with a check for IsPostBack:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
'Initialize initial values of textbox and dropdownlist here
End If
End Sub

If you don't, each time the page postsback, the controls will be reset back to their initial state, probably blank for the textbox and no selected item for the drop down list. Remember that when the page posts back, Page_Load will run BEFORE any of the web control event handlers.

0 comments:

Post a Comment