pki15
July 14th, 2003, 04:36 PM
Hi, I'm trying to design a web part for SharePoint Portal Server v2, with C#. Web Parts are essentially ASP.NET custom controls, so I thought this would be an appropriate site to post on. I want this web part to basically be a countdown to whatever date the user specifies in the properties. I decided to try a javascript implementation of this, but I'm having problems getting the Javascript to work. When I try to reference the form I set up in the Render method, the Javascript doesn't recognize it. For example, when I use the call document.clock, which is my form, it isn't recognized as a valid command. Can anyone help me figure this out? I thought maybe it had to do with the nesting inside all the tables and other forms and stuff that the Portal Server sets up for you, but I can't find a quick remedy for this. Thanks for the help, I included my code below.
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace MyWebParts
{
[ToolboxData("<{0}:CountdownWebPart runat=server></{0}:CountdownWebPart>"),
XmlRoot(Namespace="MyWebParts")]
public class CountdownWebPart : WebPart
{
public CountdownWebPart()
{
this.PreRender += new EventHandler(OnPreRender);
}
public enum Month
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
Month month = Month.January;
int day = 1;
int year = 2004;
[Browsable(true),Category("Countdown"),
DefaultValue(Month.January),
WebPartStorage(Storage.Personal),
FriendlyName("Month"),Description("Month")]
public Month M
{
get
{
return month;
}
set
{
month = value;
}
}
[Browsable(true),Category("Countdown"),
DefaultValue(1),
WebPartStorage(Storage.Personal),
FriendlyName("Day"),Description("Day")]
public int D
{
get
{
return day;
}
set
{
day = value;
}
}
[Browsable(true),Category("Countdown"),
DefaultValue(2004),
WebPartStorage(Storage.Personal),
FriendlyName("Year"),Description("Year")]
public int Y
{
get
{
return year;
}
set
{
year = value;
}
}
//string date = month.ToString() + day.ToString() + year.ToString();
private void OnPreRender(object sender, EventArgs e)
{
string scriptblock = @"
<SCRIPT LANGUAGE=""JavaScript"">
<!--
var eventdate = new Date(""" + month.ToString() + " " + day.ToString() + ", " + year.ToString() + @"00:00:01 CDT"");
function toSt(n)
{
s=""""
if(n<10)
s+=""0""
return s+n.toString();
}
function countdown()
{
cl=document.clock;
d = new Date()
count=Math.floor((eventdate.getTime()-d.getTime())/1000);
if(count<=0)
{
cl.days.value =""----"";
cl.hours.value=""--"";
cl.mins.value=""--"";
cl.secs.value=""--"";
return;
}
cl.secs.value=toSt(count%60);
count=Math.floor(count/60);
cl.mins.value=toSt(count%60);
count=Math.floor(count/60);
cl.hours.value=toSt(count%24);
count=Math.floor(count/24);
cl.days.value=count;
setTimeout(""countdown()"",500);
}
// end hiding script-->
</SCRIPT>";
if(!Page.IsClientScriptBlockRegistered("clientScript"))
{
Page.RegisterClientScriptBlock("clientScript", scriptblock);
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(@"
<BODY onLoad=""countdown()"">
<CENTER><FORM name=""clock"">
<CENTER><TABLE BORDER=2 CELLSPACING=0 CELLPADDING=5 BGCOLOR=""#000000"" >
<TR BGCOLOR=""#cccccc"">
<TD ALIGN=CENTER WIDTH=""31%""><B><FONT COLOR=""#000000"">Days:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Hours:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Mins:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Secs:</FONT></B></TD>
</TR>
<TR BGCOLOR=""#cccccc"">
<TD ALIGN=CENTER><INPUT name=""days"" size=4></TD>
<TD ALIGN=CENTER><INPUT name=""hours"" size=2></TD>
<TD ALIGN=CENTER><INPUT name=""mins"" size=2></TD>
<TD ALIGN=CENTER><INPUT name=""secs"" size=2></TD>
</TR>
</TABLE></CENTER>
</FORM></CENTER></BODY>");
}
}
}
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace MyWebParts
{
[ToolboxData("<{0}:CountdownWebPart runat=server></{0}:CountdownWebPart>"),
XmlRoot(Namespace="MyWebParts")]
public class CountdownWebPart : WebPart
{
public CountdownWebPart()
{
this.PreRender += new EventHandler(OnPreRender);
}
public enum Month
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
Month month = Month.January;
int day = 1;
int year = 2004;
[Browsable(true),Category("Countdown"),
DefaultValue(Month.January),
WebPartStorage(Storage.Personal),
FriendlyName("Month"),Description("Month")]
public Month M
{
get
{
return month;
}
set
{
month = value;
}
}
[Browsable(true),Category("Countdown"),
DefaultValue(1),
WebPartStorage(Storage.Personal),
FriendlyName("Day"),Description("Day")]
public int D
{
get
{
return day;
}
set
{
day = value;
}
}
[Browsable(true),Category("Countdown"),
DefaultValue(2004),
WebPartStorage(Storage.Personal),
FriendlyName("Year"),Description("Year")]
public int Y
{
get
{
return year;
}
set
{
year = value;
}
}
//string date = month.ToString() + day.ToString() + year.ToString();
private void OnPreRender(object sender, EventArgs e)
{
string scriptblock = @"
<SCRIPT LANGUAGE=""JavaScript"">
<!--
var eventdate = new Date(""" + month.ToString() + " " + day.ToString() + ", " + year.ToString() + @"00:00:01 CDT"");
function toSt(n)
{
s=""""
if(n<10)
s+=""0""
return s+n.toString();
}
function countdown()
{
cl=document.clock;
d = new Date()
count=Math.floor((eventdate.getTime()-d.getTime())/1000);
if(count<=0)
{
cl.days.value =""----"";
cl.hours.value=""--"";
cl.mins.value=""--"";
cl.secs.value=""--"";
return;
}
cl.secs.value=toSt(count%60);
count=Math.floor(count/60);
cl.mins.value=toSt(count%60);
count=Math.floor(count/60);
cl.hours.value=toSt(count%24);
count=Math.floor(count/24);
cl.days.value=count;
setTimeout(""countdown()"",500);
}
// end hiding script-->
</SCRIPT>";
if(!Page.IsClientScriptBlockRegistered("clientScript"))
{
Page.RegisterClientScriptBlock("clientScript", scriptblock);
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(@"
<BODY onLoad=""countdown()"">
<CENTER><FORM name=""clock"">
<CENTER><TABLE BORDER=2 CELLSPACING=0 CELLPADDING=5 BGCOLOR=""#000000"" >
<TR BGCOLOR=""#cccccc"">
<TD ALIGN=CENTER WIDTH=""31%""><B><FONT COLOR=""#000000"">Days:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Hours:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Mins:</FONT></B></TD>
<TD ALIGN=CENTER WIDTH=""23%""><B><FONT COLOR=""#000000"">Secs:</FONT></B></TD>
</TR>
<TR BGCOLOR=""#cccccc"">
<TD ALIGN=CENTER><INPUT name=""days"" size=4></TD>
<TD ALIGN=CENTER><INPUT name=""hours"" size=2></TD>
<TD ALIGN=CENTER><INPUT name=""mins"" size=2></TD>
<TD ALIGN=CENTER><INPUT name=""secs"" size=2></TD>
</TR>
</TABLE></CENTER>
</FORM></CENTER></BODY>");
}
}
}