PC Review


Reply
Thread Tools Rate Thread

Client side script

 
 
Parrot
Guest
Posts: n/a
 
      29th Oct 2008
Is there any way to execute a javascript or any kind of script when
responding to a SelectionChange event in a Calendar control? I would like to
display a window containing dynamic data on top of the parent window when
someone clicks on a date in my calendar control. I know that I can create a
script dynamically in the event but I don't know how to display the script at
that point.
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      29th Oct 2008
Depends on if you want to do this before postback or after postback.

The easy way is to do this after postback. To run a script right
after the form has been "Re-rendered" to the client, you need to
register a client startup script. If you only want this startup
script to run only when the server side OnSelectionChange event has
fired, you can use code similar to this:

protected void Calendar1_SelectionChanged(object sender,
EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"Popup", "alert('After Post');", true);
}

If you want to run JavaScript before postback, then you have to
understand the calendar control better. The calendar control renders
to an html table where all the dates are html anchors. And each
anchors href contains JavaScript already. They all look similar to
this: <a href="javascript:__doPostBack('Calendar1','3219')"

The __doPostBack function is an ASP.NET mechanism to connect generic
postbacks with server side code attached to the event.

An easy way to inject your JavaScript into the href before the
postback is:

<script language=JavaScript>

var m = document.getElementById("Calendar1");

var n = m.getElementsByTagName("a");

for (i = 0; i < n.length; i++)
{
n.item(i).href = n.item(i).href.replace("__",
"alert('Before Post');__");
}

</script>

Here we get a "pointer" to the Calendar (html table) control, then we
get all the anchor tags and walk through each one, injecting our
JavaScript before the postback.

Hope this helps,

L. Lee Saunders
http://oldschooldotnet.blogspot.com


 
Reply With Quote
 
Parrot
Guest
Posts: n/a
 
      29th Oct 2008
Lee;
Thanks for your concise reply. I will try again the startup script you
suggested. Through many attempts of solving my problem I think that I tried
the startup script but would not execute. I will try it again. What I am
trying to do is when a Selection Change event occurs, grab the date that was
clicked, use this date to fetch data from an SQL database and then through a
script display the events for that day in a window that can be closed by the
user after viewing it. This is a common use that I have seen in other web
sites and would like to do it for my web site.
Dave

"Lee" wrote:

> Depends on if you want to do this before postback or after postback.
>
> The easy way is to do this after postback. To run a script right
> after the form has been "Re-rendered" to the client, you need to
> register a client startup script. If you only want this startup
> script to run only when the server side OnSelectionChange event has
> fired, you can use code similar to this:
>
> protected void Calendar1_SelectionChanged(object sender,
> EventArgs e)
> {
> ClientScript.RegisterStartupScript(this.GetType(),
> "Popup", "alert('After Post');", true);
> }
>
> If you want to run JavaScript before postback, then you have to
> understand the calendar control better. The calendar control renders
> to an html table where all the dates are html anchors. And each
> anchors href contains JavaScript already. They all look similar to
> this: <a href="javascript:__doPostBack('Calendar1','3219')"
>
> The __doPostBack function is an ASP.NET mechanism to connect generic
> postbacks with server side code attached to the event.
>
> An easy way to inject your JavaScript into the href before the
> postback is:
>
> <script language=JavaScript>
>
> var m = document.getElementById("Calendar1");
>
> var n = m.getElementsByTagName("a");
>
> for (i = 0; i < n.length; i++)
> {
> n.item(i).href = n.item(i).href.replace("__",
> "alert('Before Post');__");
> }
>
> </script>
>
> Here we get a "pointer" to the Calendar (html table) control, then we
> get all the anchor tags and walk through each one, injecting our
> JavaScript before the postback.
>
> Hope this helps,
>
> L. Lee Saunders
> http://oldschooldotnet.blogspot.com
>
>
>

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      29th Oct 2008
Dave

Well post again (Or send me an email) if you are still having problems
- your senario is a common one and the startup script should work just
fine.

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
Reply With Quote
 
Parrot
Guest
Posts: n/a
 
      29th Oct 2008
Lee;
I recoded my startup script and it works fine. I must have coded an invalid
script the first time around. If you have any syntax errors in your script
it will not display at all. This feature really adds more power to my web
site. Thanks again for your help.
Dave

"Lee" wrote:

> Dave
>
> Well post again (Or send me an email) if you are still having problems
> - your senario is a common one and the startup script should work just
> fine.
>
> L. Lee Saunders
> http://oldschooldotnet.blogspot.com
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Server Side code in a Client Side Script??? =?Utf-8?B?TWlrZSBSYW5k?= Microsoft ASP .NET 1 30th Jan 2006 08:52 PM
Server-side script with input parameter from Client-side script Magnus Blomberg Microsoft ASP .NET 3 14th Apr 2005 01:21 PM
execute client side script + server side code for button click =?Utf-8?B?a2s=?= Microsoft C# .NET 1 17th Mar 2005 12:20 AM
get image side via client side script before file uplaod moondaddy Microsoft ASP .NET 2 16th Jul 2004 05:50 AM
Help Needed!!! client-side script, server-side code Shawn Mehaffie Microsoft ASP .NET 4 22nd Jan 2004 05:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:27 PM.