Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

running javascript when textbox value has changed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dmitry Duginov - 12 Dec 2007 00:45 GMT
Hi,

I need to clear the value in one textbox (dataEta) when user is changing the
value in another one (dataPickUpDate)

The following code works fine for typing in the textbox and cut-n-pasting
into it (onkeypress and onchange events fire)

protected void FormView1_DataBound(object sender, EventArgs e)
{
   if (FormView1.CurrentMode == FormViewMode.Edit)
   {
       TextBox pickuptextbox =
(TextBox)FormView1.FindControl("dataPickUpDate");
       TextBox ETAtextbox = (TextBox)FormView1.FindControl("dataEta");
       pickuptextbox.Attributes.Add("onchange",
String.Format("clearETA('{0}');", ETAtextbox.ClientID));
       pickuptextbox.Attributes.Add("onkeypress",
String.Format("clearETA('{0}');", ETAtextbox.ClientID));
   }
}

...
<script language=javascript type="text/javascript">
function clearETA(TextBox)
{
    var textboxETA = document.getElementById(TextBox);
    if (textboxETA!=null)
       textboxETA.value='';
}
</script>

However, user also can select the new value using calendar pop-up control:

<asp:TextBox runat="server" ID="dataEta"
Text='<%# Bind("Eta", "{0:d}") %>' />
<asp:ImageButton ID="cal_dataPickUpDate" runat="server"
OnClientClick = "javascript:showCalendarControl(this.previousSibling);return
false;"/>

Of course, in this case neither onchange nor onkeypress fires.

How would I ensure that clearETA() javascript function called when the user
selects the date from the calendar pop-up? I am reluctant to make changes in
showCalendarControl() because it is used application-wide and usually this
extra functionality is not required. What are my options?

D.
Michael Nemtsev - 12 Dec 2007 02:15 GMT
Does your callendar have events like "OnDataSelected" or smth?
Another option is to call clearETA() manually after showCalendarControl method

Signature

WBR,  Michael  Nemtsev [.NET/C# MVP].  
Blog: http://spaces.live.com/laflour

> Hi,
>
[quoted text clipped - 44 lines]
>
> D.
Dmitry Duginov - 12 Dec 2007 14:18 GMT
> Does your callendar have events like "OnDataSelected" or smth?

No events, it is 100% javascript

> Another option is to call clearETA() manually after showCalendarControl
> method

That's what I'd like to do. But I cannot figure out how to pass the correct
ClientID as a parameter to clearETA() in markup.

The following works:

<asp:TextBox
runat="server"
ID="dataPickUpDate"
Text='<%# Bind("PickUpDate", "{0:d}") %>'
/>

<asp:ImageButton
ID="cal_dataPickUpDate"
runat="server"
OnClientClick =
"javascript:showCalendarControl(this.previousSibling);clearETA('ctl00_ContentPlaceHolder1_FormView1_dataEta');return
false;"
/>

But for obvious reasons I cannot hardcode
'ctl00_ContentPlaceHolder1_FormView1_dataEta' as ClientId

And I cannot use the following markup...

OnClientClick =
"javascript:showCalendarControl(this.previousSibling);clearETA('<%=FormView1.FindControl("dataEta").ClientID%>');return
false;"

...because when it is evaluated before FormView is databound...

Please advise.

D.

>> Hi,
>>
[quoted text clipped - 50 lines]
>>
>> D.
Steven Cheng[MSFT] - 13 Dec 2007 02:20 GMT
Hi Dmitry,

If you're wondering how to pass the TextBox's clientID into the
ImageButton's client-side script, how abou the following approach:

You can add "PreRender" event handler for the ImageButton control, in that
event, you can get the ClientID from the certain Textbox and then
programmatically set imagebutton's "OnClientclick" property?  

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Dmitry Duginov" <dima@nospam.nospam>
>References:  <eXR6KhFPIHA.4912@TK2MSFTNGP06.phx.gbl>
<2E65CBBD-D1EE-4CA5-B6D2-FDB046E908F3@microsoft.com>
>Subject: Re: running javascript when textbox value has changed
>Date: Wed, 12 Dec 2007 09:18:51 -0500
[quoted text clipped - 94 lines]
>>>
>>> D.
Dmitry Duginov - 13 Dec 2007 03:28 GMT
> Hi Dmitry,
>
[quoted text clipped - 4 lines]
> event, you can get the ClientID from the certain Textbox and then
> programmatically set imagebutton's "OnClientclick" property?

Great, thanks. I've placed it together with two other calls for this script.
Works fine.

   protected void FormView1_DataBound(object sender, EventArgs e)
   {
       if (FormView1.CurrentMode == FormViewMode.Edit)
       {
           TextBox pickuptextbox =
(TextBox)FormView1.FindControl("dataPickUpDate");
           TextBox ETAtextbox = (TextBox)FormView1.FindControl("dataEta");
           ImageButton pickupCalendarButton =
(ImageButton)FormView1.FindControl("cal_dataPickUpDate");
           pickuptextbox.Attributes.Add("onchange",
String.Format("clearETA('{0}');", ETAtextbox.ClientID));
           pickuptextbox.Attributes.Add("onkeypress",
String.Format("clearETA('{0}');", ETAtextbox.ClientID));
           pickupCalendarButton.OnClientClick =
               String.Format("javascript:showCalendarControl(this.previousSibling);clearETA('{0}');return
false;", ETAtextbox.ClientID);
       }
   }

Regards,
D.

> Sincerely,
>
[quoted text clipped - 113 lines]
>>>>
>>>> D.
Steven Cheng[MSFT] - 12 Dec 2007 04:45 GMT
Hi Dmitry,

I agree with Michael  that the proper chance to make your textbox get
updated is something in the datetimepicker control. As it will raise a
popup windows for date picking, is there any chance that we can register
script handler or add script into the date time picking period so as to
call a script function after the user finishing select the date?  Elsewise,
it will be hard to automatically detect window popup from textbox's view.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
   

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Dmitry Duginov" <dima@nospam.nospam>
>Subject: running javascript when textbox value has changed
[quoted text clipped - 48 lines]
>
>D.
Dmitry Duginov - 12 Dec 2007 14:21 GMT
> Hi Dmitry,
>
[quoted text clipped - 5 lines]
> Elsewise,
> it will be hard to automatically detect window popup from textbox's view.

The actual problem here is how to pass proper ClientId for the textbox to
clear to the javascript declared in OnClientClick. Please see my reply to
Michael...

D.

> Sincerely,
>
[quoted text clipped - 62 lines]
>>
>>D.
Oren - 12 Dec 2007 08:32 GMT
you probably don't want to use OnDataSelected because the Roundtrip, write ?

> Hi,
>
[quoted text clipped - 44 lines]
>
> D.
Dmitry Duginov - 12 Dec 2007 14:24 GMT
> you probably don't want to use OnDataSelected because the Roundtrip, write
> ?

It is not ASP.NET Calendar control, it's built on the fly by 100%
javascript.

D.

>> Hi,
>>
[quoted text clipped - 50 lines]
>>
>> D.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.