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

Who is Participating?
NazoUKConnect With a Mentor Commented:
Thinking about it, probably a better solution is taking our the readonly = true from the server tag and in your page load or prerender put EndDt.Attributes("readonly") = "readonly"
This will set the client side readonly property but won't affect the server side processing so you should see the changes reflected in EndDt.text
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.
escheiderAuthor Commented:

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()
        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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
escheiderAuthor Commented:
I changed it to request the form object values and that worked ..   I'm not sure where to check now.
escheiderAuthor Commented:
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.
escheiderAuthor Commented:
Thanks for the input ..  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.