Saturday, March 31, 2012

Text box TextChangedEvent not firing

I have a textbox box and a date picker.When i select a date through
the date picker, it fills the textbox with the selected date.Now, i
have a text changed event for the textbox. This event fires only if i
key in a date manually and not if i use the date picker.
How can i make the textchanged event to fire when i use the date
picker?When a date is selected using the date picker and it fills the TextBox, is
this done using a postback or using JavaScript? If it is done using a
postback, the TextChanged event will not be triggered because the text
didn't change since the last postback. If this is the case, here are several
options:
Option #1: Get the date from the datepicker, this way you can get it as a
Date rather than need to worry about converting a String into a Date. If you
want to allow the user to enter the date manually instead, try following
this process (you may need to add a little validation to make sure you do
not get an error converting from String to Date):
1. Get the date from the date picker
2. In the TextChanged event, use the date from the TextBox
Option #2: You can also try using AJAX, which would be my choice.
Option #3: Write your own control.
Good Luck!
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"Sue" <iyer_sansid@.hotmail.com> wrote in message
news:3d8d8c6d-0253-4136-85dc-00a7c24f3ac9@.q78g2000hsh.googlegroups.com...
> I have a textbox box and a date picker.When i select a date through
> the date picker, it fills the textbox with the selected date.Now, i
> have a text changed event for the textbox. This event fires only if i
> key in a date manually and not if i use the date picker.
> How can i make the textchanged event to fire when i use the date
> picker?
>
On 21 Mar, 20:39, Sue <iyer_san...@.hotmail.com> wrote:
> =A0I have a textbox box and a date picker.When i select a date through
> the date picker, it fills the textbox with the selected date.Now, i
> have a text changed event for the textbox. This event fires only if i
> key in a date manually and not if i use the date picker.
> How can i make the textchanged event to fire when i use the date
> picker?
Hi
Only one control can trigger a PostBack at any one time and hence
"fire" it's event handler.
No problem!
If you want a piece of code to be executed on the server in response
to more than one control then write a separate routine to handle it
and then put a statement to call the routine in the event handlers of
each control.
For example:
private void SaveDate(DateTime ADate)
{
//code to save date to datasource or whatever
}
protected TextBox1_TextChanged(object sender, EventArgs e)
{
DateTime inputDate =3D DateTime.Parse(TextBox1.Text);
SaveDate(inputDate);
Calendar1.SelectedDate =3D inputDate;
Calendar1.VisibleDate =3D inputDate;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
SaveDate(Calendar1.SelectedDate);
TextBox1.Text =3D Calendar1.Date.ToShortDateString();
}
HTH

0 comments:

Post a Comment