How do I disable dates in asp .net textbox textmode = date? - c#

I'm having this issue where I want to set disable on the dates in the textbox with the textmode = date. I would prefer it to be done in c#.
Any advice?
Here is the aspx page:
<asp:TextBox ID="tbStartDate" runat="server"
AutoPostBack="true" OnTextChanged="tbStartDate_TextChanged"
TextMode="Date"></asp:TextBox>
And the C# file
protected void disablePastDateintextbox()
{
????????
}

Related

Date selected from calendar control doesn't change in Textbox at runtime

I have to save date selected from ajax calender control,when i select date from ajax calender control it shows in textbox,but when i save this date i got the previous value of date not the cureently selected value,i have written this code on btnsave_Click event
my .CS code is as follow:
protected void btnsave_Click(object sender, EventArgs e)
{
DateTime bdate = DateTime.ParseExact(txtBirthDate.Text, "dd/MM/yyyy", null);
}
my .aspx code for calender control:
<td>
<asp:TextBox ID="txtBirthDate" runat="server" ReadOnly="true" CssClass="Txtprop" ></asp:TextBox>
<cc1:CalendarExtender ID="calDOB" runat="server" TargetControlID="txtBirthDate" Format="dd/MM/yyyy" ></cc1:CalendarExtender>
</td>
You should not set the property ReadOnly="true" on your TextBox.
If TextBox's ReadOnly property is "true", postback data won't be loaded e.g it essentially means TextBox being readonly from server-side standpoint (client-side changes will be ignored). If you want TB to be readonly in the "old manner" use
TextBox1.Attributes.Add("readonly","readonly")
as that won't affect server-side functionality.
For more information follow StackoverflowAnswer or TextBox Readonly
problem.
Why don't you set the date time format in .cs file itself.
Look here
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.customformat%28v=vs.110%29.aspx
before <asp:Content> add this line
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
After <asp:Content> add
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
To solve this set the CalendarExtender's SelectedDate in the textbox.Text data = change to whatever it is in the textbox and it will stick. Setting SelectedDate sets the textbox field at the same time.
Easy fix example
'protected void txDate_TextChanged(object sender, EventArgs e)
{
txDate_CalendarExtender.SelectedDate = Convert.ToDateTime(txDate.Text);
}'

How Do I Generate Dynamic Contols Based On Selected Months From Ajaxtoolkit:Calendarextender?

In my .aspx page, Value appears in text box based on selected month from ajaxToolkit:CalendarExtender.
I have to generate dynamic labels and textboxes based on selected month's days.
I will generate dynamic controls based on selected month.
but how can I fire event/function in aspx.cs file just after select the month from ajaxToolkit:CalendarExtender.
I have to tried to find the solution in google from last 2 day but without any success.
In aspx page
<asp:TextBox runat="server" ID="txtHoliDate" AutoPostBack="True" OnTextChanged="txtHoliDate_TextChanged" />
<asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/Admin/Images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" Height="15px" CausesValidation="False" /><br />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtHoliDate" PopupButtonID="Image1" TodaysDateFormat="d MMMM, yyyy" Format="dd-MM-yyyy" CssClass="clsCalendar" />
in aspx.cs file
protected void txtHoliDate_TextChanged(object sender, EventArgs e)
{
// Some code here...
}

How to bind calender selected value to a txt bx

I am trying to create an ajax popup calender. So When the user clicks a image button the calender should pop up and the selected date from the calender should be bind to a text box.
This is my aspx code:
<asp:TextBox ID="txtBxDate" runat="server" MaxLength="10" Width="75px">
</asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/toolbox.gif" />
<asp:PopupControlExtender ID="ImageButton1_PopupControlExtender" runat="server"
DynamicServicePath="" Enabled="True" ExtenderControlID=""
PopupControlID="Panel1" Position="Bottom" TargetControlID="ImageButton1">
</asp:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server" Width="200px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
My Code Behind:
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtBxDate.Text = Calendar1.SelectedDate.ToString();
}
But when I selected a date from the calender, the selected date wasn't binded to the text box. What I am doing wrong here? or how can I fix it?
The reason it's not updating is because txtBxDate is outside of the UpdatePanel. Either put txtBxDate in the update panel with the calendar, or put it in a separate update panel.
EDIT
After inspecting your code a little more closely, it looks like you're trying to manually create the functionality that's already built into the AJAX Toolkit Calendar control.
See here: Calendar Sample
It seems that you have your targetcontrolid and popupcontrolid mixed up. I think it should be:
PopupControlID="ImageButton1" TargetControlID="txtBxDate"
You can get rid of the code-behind setting the selected date on the text field. The calendar extender should take care of that automatically.

OnClick event in Textbox

I have a textbox in the webpage.
//.aspx
<td class="style2">Start of Date:<asp:TextBox ID="TextBox1" runat="server"
Height="22px"
ontextchanged="TextBox1_TextChanged" Width="157px"></asp:TextBox>
</td>
//aspx.cs
protected void cntCalendar_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = cntCalendar.TodaysDate.ToShortDateString();
}
Now I'm able to take the value in the textbox from calendar,but my problem is I'm not able to get the calendar to show onclick in the textbox and hide after date selected.
Can anybody help me with this please.
Use the CalendarExtender class from the ASP.NET AJAX ToolKit.
Download and usage
You shouldn't do the post backs for the selecting date.
Sounds like you want to use something like the AJAX calendar extender. This control will negate the need of the asp:Calendar control.
See below for an example.
<asp:TextBox ID="txtStartDate" runat="server" CausesValidation="true" >
</asp:TextBox>
<ajax:CalendarExtender ID="ce1" runat="server" Format="dd-MM-yyyy" TargetControlID="txtStartDate" PopupPosition="Right" >
</ajax:CalendarExtender>
The calendar will popup on click and when a date is selected, the textbox will be populated with the date selected in the format specified in the Format attribute
VMAtm is right on with the CalendarExtender.
However, if you have your own custom calendar you're using and you just need to manually pop that up via client onclick of the textbox, do this in Page_Load or PreRender:
TextBox1.Attributes["onclick"] = "showMyCalendar();";
or
TextBox1.Attributes[HtmlTextWriterAttribute.Onclick] = "showMyCalendar();";

How to validate years dynamically using validators in ASP.Net?

I have to validate that the year entered must be less than or equals to the current year. I am using server side validators in ASP.Net and C#, and all validators are inside a validator group. So please suggest me to validate one more condition within the same.
Thanks
Amit Ranjan.
Use the ASP.NET CompareValidator.
For e.g. if you have a TextBox with id TextBox1, which accepts a year in 'yyyy' format, then you can do the following:
Year:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator id="CompareValidator1" runat="server"
Operator="LessThan" ControlToValidate="TextBox1" ErrorMessage="Year must
be less than current year" Type="Integer"></asp:CompareValidator>
Add this to code behind:
protected void Page_Load(object sender, EventArgs e)
{
CompareValidator1.ValueToCompare = DateTime.Today.Year;
}
Also remember using validators won't stop a Submit; use these in conjunction with ValidationSummary Control.
More on MSDN: http://msdn.microsoft.com/en-us/library/f9h59855(v=vs.80).aspx

Categories