In this article I will explain with an example, how to get selected date from
ReadOnly TextBox while using
AJAX CalendarExtender control in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consist of following controls:
ScriptManager – For enabling ASP.Net
AJAX.
TextBox – For submitting the user input.
ImageButton – For displaying calendar icon.
CalendarExtender – For displaying calendar.
The
AJAX CalendarExtender has been set assigned with the following properties:
TargetControlID – The control where the Text will displayed.
PopupButtonID – The control which opens the Calendar.
Button – For capturing selected Date from the TextBox.
The Button has been assigned with an OnClick event handler.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:TextBox ID="txtDate" runat="server" ReadOnly="true" />
<asp:ImageButton ID="imgPopup" runat="server" ImageUrl="~/Image/calendar.gif" />
<ajaxtoolkit:calendarextender ID="Calender1" runat="server" TargetControlID="txtDate" PopupButtonID="imgPopup" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
Issue
The selected date in ReadOnly TextBox cannot be fetched using Text property of the TextBox as shown below.
protected void Submit(object sender, EventArgs e)
{
string selectedDate = txtDate.Text;
}
Screenshot
Reason
Whenever the value of a TextBox is set to
ReadOnly and the value of the TextBox is set on Client-Side using client side script like
JavaScript, then the value of the Textbox is not available in the
Text property of the TextBox.
Solution
The Solution is to make use of
Request.Form collection 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.
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 date As String = Request.Form(txtDate.UniqueID)
End Sub
Screenshot
Downloads