Wednesday, October 15, 2014

Text Box should accepts only date using Calendar Extender

Sometimes we need validation for date, that user can only put the date in textbox.

Here is a method through which user can only select the date from calendar extender. User can't type, paste etc. in textbox.

Below is my TextBox along with Calendar Extender.


 <asp:TextBox ID="txtDate" runat="server" CssClass="mytextbox" Enabled="False"></asp:TextBox>  
         <asp:CalendarExtender ID="txtDate_CalendarExtender" runat="server"   
           Enabled="True" TargetControlID="txtDate" Format="dd/MM/yyyy">  
         </asp:CalendarExtender>  

On the Page_Load event of this form you just need to add a attribute to the TextBox.

  protected void Page_Load(object sender, EventArgs e)  
   {  
     if (!IsPostBack)  
     {  
         txtDate.Attributes.Add("readonly", "readonly");  
     }  
   }  

Now you can only select date from calendar extender. You can't type, paste etc. in textbox.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...