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

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);
}'

Related

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

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()
{
????????
}

Data Binding label control in asp.net

I have added a calender control to my Webform and on date selection change I have to display the selected date in a label control
<asp:Calendar ID="calendar1" runat="server"
onselectionchanged="calendar1_SelectionChanged" >
</asp:Calendar>
<asp:Label ID="lblInfo" runat="server" Visible="true"
Text="<%#calendar1.SelectedDate.ToShortDateString()%>">
</asp:Label>
But this is not working? Do I need to call any methods in code behind?I dont understand why this is not working.
As noted in the comment section, In your markup add an eventhandler for OnSelectionChanged for the calendar control.
<asp:Calendar ID="calendar1" runat="server"
OnSelectionChanged="calendar1_SelectionChanged" >
</asp:Calendar>
and then in your code behind, handle the event
void calendar1_SelectionChanged(Object sender, EventArgs e)
{
lblInfo.Text= calendar1.SelectedDate.ToShortDateString();
}
As the SelectionChanged event of 'calendar1' is handled by 'calendar1_SelectionChanged',
So, at the code behind, the function should be --->
private void calendar1_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
lblInfo.Text= calendar1.SelectedDate.ToShortDateString();
}
The html markup renders for the first time, and it's done. But, events pertaining to those controls need to be handled in the 'code behind'

FindControl TextBox which don't get the good value Sharepoint

In my asp page i got :
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:DropDownList ID="_SCP_ddlStatutDelais" runat="server"></asp:DropDownList>
<asp:TextBox ID="_SCP_tbTypeMiseProduction" Rows="3" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="_btSend" runat="server" Text="Envoyer" CssClass="ms-ButtonHeightWidth"
onclick="_btSend_Click"/>
</asp:Content>
Then, in my code behind, i get values from database to provide my TextBox and DDL in my Page_Load, and it works.
Then, i want to update my database with values modify by user, so i try to get the Text in TextBox but i can only got the Text that i put from my database, and myTextBox.Text ignore Text modify by user.
Code Behind :
protected void _btSend_Click(object sender, EventArgs e)
{
Control context = this.Page.Master.FindControl("PlaceHolderMain");
//Informations Database Connection etc...
reflector.Set(d[fieldtomap],rootTypeDescriptor, ref instance, ((TextBox)(context.FindControl(nodeName))).Text);
//Submit update to database
}
For example if i get from my database : "Test", i put in my TextBox "Test". Then user modify this value then validate with the button, ((TextBox)(context.FindControl(nodeName))).Text contains always "Test" and ignore user's modification.
Are you checking for Page.IsPostback when binding your data? You should only be binding on the initial page load, else, the change are overwritten - just like you are experiencing.

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

Categories