ASP.Net AJAX CalendarExtender - Get selected date from ReadOnly TextBox
 
Published Date: Feb 24, 2011
Views: 5141
 

Abstract: Here Mudassar Ahmed Khan has explained how to get selected date value when the textbox is set to readonly

Comments:  5

 

In this article I will explain a very common issue faced while using the ASP.Net AJAX Control Toolkit CalendarExtender control.
Issue
Whenever for the ASP.Net Textbox if the ReadOnly property of the Textbox is set to true, the selected data is not available in the Text property of the Textbox on PostBack as shown in the screenshot below.
 
ASP.Net AJAXControlToolkit CalendarExtender Readonly Textbox value not available

Reason
 
The reason behind this issue is that whenever value of an ASP.Net Textbox is set to ReadOnly and the value of the textbox is set client side using client side script like JavaScript, in such cases the value of the Textbox is not available in the Text property of the ASP.Net Textbox. The ASP.Net AJAX Control Toolkit CalendarExtender makes use of JavaScript to set the selected date in the Textbox.
 
Solution
 
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
 
Thus we need to do a small change in the way we are fetching the value server side.
 
C#
 
protected void Submit(object sender, EventArgs e)
{
    string date = Request.Form[txtDate.UniqueID];
}
 
VB.Net
 
Protected Sub Submit(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim strDate As String = Request.Form(txtDate.UniqueID)
End Sub
 
As you can see above I am fetching the value of the Textbox from the Request.Form collection using the UniqueID property of the Textbox which is nothing but Client Side name of the ASP.Net TextBox control
The screenshot below describes that the selected date value is now available server side when fetched using the Request.Form collection
 
ASP.Net AJAXControlToolkit CalendarExtender Readonly Textbox value available server side

That’s it from me, hope you like this article.

 








Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit