Link to home
Start Free TrialLog in
Avatar of escheider
escheider

asked on

Updating Textbox Server Control with Javascript

Good evening Experts:

I am having some difficulties updating an asp textbox server control with a Javascript routine.  In my situation, the user has the ability to populate two textboxes with a Javascript calendar control [BeginDt and EndDt].  This part works fine --  the user selects the date and the textbox control is populated; however, when the webform is submitted, the dates selected using the Javascript routine are not being reflected after the postback.      For example, if the BeginDt control displays 02/01/2008, and the user selects 02/03/2008 from the calendar control, the BeginDt textbox will display the change, but when the form is submitted, and the postback is complete, the original value [02/01/2008] is displayed.

When I debug the application and step through the code, the value in BeginDt.Text and EndDt.Text are displaying the original value prior to updating them with the Javascript control.

I am using VB.NET 2008 ..  Any help would be appreciated.
clsCampaign.CommunityNumber = CommunityNumber.Value
clsCampaign.CampaignName = CampaignName.Text
clsCampaign.CampaignDescription = CampaignDescription.Text
clsCampaign.Sort = Sort.Text
clsCampaign.EndDT = EndDt.Text   <-- Displaying original value 
clsCampaign.BeginDT = BeginDt.Text   <-- Displaying original value

Open in new window

Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you populating the textbox with a default value in page_load or something similar? Make sure any default value population code is wrapped in a If not page.IsPostBack then ... block otherwise the textbox values will be reset back before any control events fire.
Avatar of escheider
escheider

ASKER

Nazo:

Yes, the private sub that is being referenced in the page_load event is encapsulated within a ispostback conditional:


If Not IsPostBack Then
            If Len(Trim(clsCampaign.CampaignID)) > 0 Then
                DataToFormBind()  <-- populate form objects here
            End If
End If
 
 
Private Sub DataToFormBind()
        clsCampaign.CampaignDetailGet()
        
        CampaignName.Text = clsCampaign.CampaignName
        CampaignDescription.Text = clsCampaign.CampaignDescription
        BeginDt.Text = clsCampaign.BeginDT
        EndDt.Text = clsCampaign.EndDT
        Sort.Text = clsCampaign.Sort
End Sub

Open in new window

That looks ok, any other code you can post? I'm sure it must be something code related, we use a javascript based calendar, the textbox doesn't care how the text gets there, if it's there it should be in the form collection.
As a test you could change
clsCampaign.EndDT = EndDt.Text to
clsCampaign.EndDT = request.form("EndDt") (assuming your textboxes are just on the page, not in usercontrols or anything) to make sure the correct value is being posted back. If this fixes the problem you know the value is being overwritten somewhere in your asp.net code, if it doesn't the problem is possibly in your client side script somewhere.
I changed it to request the form object values and that worked ..   I'm not sure where to check now.
Interesting ..  the two textboxes where defined as read-only.  When I set that attribute to false, it works fine.  Can the value not be updated by client-side script if the control is set to read-only?
I suspect this is probably the case, same thing happened when I tested it. If you want your textboxes to be read-only then you could always put a hidden field on the page, have the javascript update that at the same time and get the value from there.
Other than that there's nothing particularly wrong with using the form values directly.
ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the input ..