Yeah, the event is being run, and the RegisterStartup Script line is
executing error free. I tried using this.Page.Regis terStartupScrip t,
this.RegisterSt artupScript, Page.RegisterSt artupScript, and just
RegisterStartup Script. When I do a view source on the rendered page, I do
not see the javascript code anywhere. I added a protected override void
OnPreRender(Eve ntArgs e) function and added break point within. When I
stepped through the code, all of the eventhandling code was executing prior
to the OnPreRender function being called, which makes me think that this
should work. Thanks again for any input.
Here is the code. One thing I did not mention before is that the checkbox
controls are being added to the page programatically in the
buildEmptyCalen dar function. The code below is the Page_Load function, the
buildEmptyCalen dar function, and the eventhandler for when the CheckChanged
event is called, timeSelect, along with the code generated by visual studio.
The line calling RegisterStartup Script is noted in the timeSelect function:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using com.actcci.sche dule.sched_bl;
namespace com.actcci.sche dule.schedules
{
/// <summary>
/// Summary description for custompattern2.
/// </summary>
public class custompattern2 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l Label2;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Butt on ResetPattern;
protected System.Web.UI.W ebControls.Butt on AddWeek;
protected System.Web.UI.W ebControls.Butt on SavePattern;
protected System.Web.UI.W ebControls.Text Box IptNumWeeks;
protected System.Web.UI.W ebControls.Tabl e PatternTable;
protected System.Web.UI.W ebControls.Butt on RemoveWeek;
protected System.Web.UI.W ebControls.Drop DownList dd_patterns;
protected System.Web.UI.W ebControls.Plac eHolder calHolder;
protected System.Collecti ons.Hashtable sessionsTable;
protected int numweeks;
private void Page_Load(objec t sender, System.EventArg s e){
numweeks = Int32.Parse(Ipt NumWeeks.Text);
if(!IsPostBack) {
buildEmptyCalen dar();
}//end if
else{
if(Session["patterntab le"] == null){
Response.Write( "Session Timed Out");
Response.End();
}//end if
else{
PatternTable = (Table)Session["patterntab le"];
}//end else
}//end else
calHolder.Contr ols.Add(Pattern Table);
}
private void buildEmptyCalen dar(){
PatternTable = new Table();
TableRow tr = this.weekHeader ();
PatternTable.Ro ws.Add(tr);
TableItemStyle tis = new TableItemStyle( );
tis.BackColor = ColorTranslator .FromHtml("#fff fff");
tis.ForeColor = ColorTranslator .FromHtml("#000 000");
tis.Width = Unit.Pixel(100) ;
tis.Height = Unit.Pixel(100) ;
tis.Font.Size = FontUnit.XSmall ;
tis.BorderColor = ColorTranslator .FromHtml("#000 000");
tis.BorderStyle = BorderStyle.Sol id;
tis.BorderWidth = Unit.Pixel(1);
listOfValues tsession = new listOfValues("t ruesession");
TableCell tc = null;
CheckBox chkbx = null;
TextBox fromtime = null;
TextBox totime = null;
string idpre = null;
for(int currweek=0;curr week<numweeks;c urrweek++){
tr = new TableRow();
for(int x=0;x<7;x++){
tc = new TableCell();
tc.ApplyStyle(t is);
for(int y=0;y<tsession. Count();y++){
chkbx = new CheckBox();
chkbx.Text = tsession.listIt em(y).Descripti on
+ "<BR>";
chkbx.AutoPostB ack = true;
chkbx.CheckedCh anged += new
System.EventHan dler(this.timeS elect);
idpre = Convert.ToStrin g((x + (currweek *
7)));
while(idpre.Len gth < 3){
idpre = "0" + idpre;
}//end while
chkbx.ID = idpre+ "chk" +
tsession.listIt em(y).Val;
tc.Controls.Add (chkbx);
}//end for
fromtime = new TextBox();
fromtime.ID = idpre + "fromtime";
totime = new TextBox();
totime.ID = idpre + "totime";
tc.Controls.Add (fromtime);
tc.Controls.Add (totime);
tr.Cells.Add(tc );
}//end for
PatternTable.Ro ws.Add(tr);
}//end for
TableStyle ts = new TableStyle();
ts.CellPadding = 0;
ts.CellSpacing = 0;
PatternTable.Ap plyStyle(ts);
Session["patterntab le"] = PatternTable;
}//end private void buildEmptyCalen dar()
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.AddWeek.Cl ick += new System.EventHan dler(this.AddWe ek_Click);
this.RemoveWeek .Click += new System.EventHan dler(this.Remov eWeek_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void timeSelect(obje ct sender, System.EventArg s e){
CheckBox chkbx = (CheckBox)sende r;
string idpre = chkbx.ID.Substr ing(0,3);
if(Session["sessionsTa ble"]==null){
sessionsTable = new Hashtable();
}//end if
else{
sessionsTable = (Hashtable)Sess ion["sessionsTa ble"];
}//end else
CheckBox oldchk = null;
string key = null;
ArrayList al = new ArrayList(sessi onsTable.Keys);
for(int x=sessionsTable .Count-1;x>=0;x--){
key = (string)al[x];
if(key.IndexOf( idpre) >= 0){
oldchk = (CheckBox)Patte rnTable.FindCon trol(key);
oldchk.Checked = false;
sessionsTable.R emove(key);
}//end if
}//end foreach
if(chkbx.Checke d){
actSession acts = new
actSession(chkb x.ID.Substring( chkbx.ID.Length - 3));
sessionsTable.A dd(chkbx.ID,act s);
TextBox tbx = (TextBox)Patter nTable.FindCont rol(idpre +
"fromtime") ;
tbx.Text = acts.fromTime.T oShortTimeStrin g();
tbx = (TextBox)Patter nTable.FindCont rol(idpre +
"totime");
tbx.Text = acts.toTime.ToS hortTimeString( );
string jscrpt = null;
if(acts.fromTim e.CompareTo(act s.toTime) == 0){
jscrpt = "<script language=\"Java Script\">";
jscrpt += "alert(\"yo yo fred\")";
jscrpt += "</script>";
string scriptKey = "windowload :" + this.UniqueID;
//HERE IS THE REGISTERSTARTUP SCRIPT LINE
RegisterStartup Script(scriptKe y,jscrpt);
}//end if
}//end if
else{
TextBox tbx = (TextBox)Patter nTable.FindCont rol(idpre +
"fromtime") ;
tbx.Text = null;
tbx = (TextBox)Patter nTable.FindCont rol(idpre +
"totime");
tbx.Text = null;
}//end else
Session["sessionsTa ble"] = sessionsTable;
Session["patternTab le"] = PatternTable;
}//end
}
}
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in message
news:uQ******** ******@tk2msftn gp13.phx.gbl...
Scott wrote:
One place you shouldn't call them is Render; as a rule I always call
them in PreRender.
Scott
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in message
news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
Bill Jones wrote:
I'm trying to use this.RegisterSt artupScript to add some javascript to
andaspx page that will run when the page is loaded. Does anyone know if
thisfunction only works in the Page_Load function? Or can I put it
anywhere.Right now it is inside the eventhandler function that is called on the
"CheckChange d" event of a checkbox, but it never seems to add the
javascriptcode to the page. Is there another way to do this? Let me explain the
problem:
I have a page with a bunch of checkboxes. Each checkbox forces a
postback.In the eventhandler function for the "CheckChang ed" event of the
checkboxesI need to determine if two values match each other. If they do, then I
wantto use some javascript to open a modal dialog to allow for the input of
twonew values. Make sense? Any help would be great! Thanks.
- Bill Jones
Yes, you can use it anywhere in code-behind; it will get added to the
response output. Are you positive the event is being run, have you
stepped thru in debug mode?
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Ah, yes, there are places you could potentially cause trouble. My
statement should be for normal page event processing, where we usually
aren't dealing with page events mostly outside of Page_Load. Some of
the other events could (may or may not) get you in trouble :-) Still
not the best description by me, but thanx for the clarification.. .
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET