Client side script

  • Thread starter Thread starter Parrot
  • Start date Start date
P

Parrot

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.
 
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
 
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;
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
 
Back
Top