Yeah, the event is being run, and the RegisterStartupScript line is
executing error free. I tried using this.Page.RegisterStartupScript,
this.RegisterStartupScript, Page.RegisterStartupScript, and just
RegisterStartupScript. 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(EventArgs 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
buildEmptyCalendar function. The code below is the Page_Load function, the
buildEmptyCalendar function, and the eventhandler for when the CheckChanged
event is called, timeSelect, along with the code generated by visual studio.
The line calling RegisterStartupScript is noted in the timeSelect function:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using com.actcci.schedule.sched_bl;
namespace com.actcci.schedule.schedules
{
/// <summary>
/// Summary description for custompattern2.
/// </summary>
public class custompattern2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button ResetPattern;
protected System.Web.UI.WebControls.Button AddWeek;
protected System.Web.UI.WebControls.Button SavePattern;
protected System.Web.UI.WebControls.TextBox IptNumWeeks;
protected System.Web.UI.WebControls.Table PatternTable;
protected System.Web.UI.WebControls.Button RemoveWeek;
protected System.Web.UI.WebControls.DropDownList dd_patterns;
protected System.Web.UI.WebControls.PlaceHolder calHolder;
protected System.Collections.Hashtable sessionsTable;
protected int numweeks;
private void Page_Load(object sender, System.EventArgs e){
numweeks = Int32.Parse(IptNumWeeks.Text);
if(!IsPostBack){
buildEmptyCalendar();
}//end if
else{
if(Session["patterntable"] == null){
Response.Write("Session Timed Out");
Response.End();
}//end if
else{
PatternTable = (Table)Session["patterntable"];
}//end else
}//end else
calHolder.Controls.Add(PatternTable);
}
private void buildEmptyCalendar(){
PatternTable = new Table();
TableRow tr = this.weekHeader();
PatternTable.Rows.Add(tr);
TableItemStyle tis = new TableItemStyle();
tis.BackColor = ColorTranslator.FromHtml("#ffffff");
tis.ForeColor = ColorTranslator.FromHtml("#000000");
tis.Width = Unit.Pixel(100);
tis.Height = Unit.Pixel(100);
tis.Font.Size = FontUnit.XSmall;
tis.BorderColor = ColorTranslator.FromHtml("#000000");
tis.BorderStyle = BorderStyle.Solid;
tis.BorderWidth = Unit.Pixel(1);
listOfValues tsession = new listOfValues("truesession");
TableCell tc = null;
CheckBox chkbx = null;
TextBox fromtime = null;
TextBox totime = null;
string idpre = null;
for(int currweek=0;currweek<numweeks;currweek++){
tr = new TableRow();
for(int x=0;x<7;x++){
tc = new TableCell();
tc.ApplyStyle(tis);
for(int y=0;y<tsession.Count();y++){
chkbx = new CheckBox();
chkbx.Text = tsession.listItem(y).Description
+ "<BR>";
chkbx.AutoPostBack = true;
chkbx.CheckedChanged += new
System.EventHandler(this.timeSelect);
idpre = Convert.ToString((x + (currweek *
7)));
while(idpre.Length < 3){
idpre = "0" + idpre;
}//end while
chkbx.ID = idpre+ "chk" +
tsession.listItem(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.Rows.Add(tr);
}//end for
TableStyle ts = new TableStyle();
ts.CellPadding = 0;
ts.CellSpacing = 0;
PatternTable.ApplyStyle(ts);
Session["patterntable"] = PatternTable;
}//end private void buildEmptyCalendar()
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.AddWeek.Click += new System.EventHandler(this.AddWeek_Click);
this.RemoveWeek.Click += new System.EventHandler(this.RemoveWeek_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void timeSelect(object sender, System.EventArgs e){
CheckBox chkbx = (CheckBox)sender;
string idpre = chkbx.ID.Substring(0,3);
if(Session["sessionsTable"]==null){
sessionsTable = new Hashtable();
}//end if
else{
sessionsTable = (Hashtable)Session["sessionsTable"];
}//end else
CheckBox oldchk = null;
string key = null;
ArrayList al = new ArrayList(sessionsTable.Keys);
for(int x=sessionsTable.Count-1;x>=0;x--){
key = (string)al[x];
if(key.IndexOf(idpre) >= 0){
oldchk = (CheckBox)PatternTable.FindControl(key);
oldchk.Checked = false;
sessionsTable.Remove(key);
}//end if
}//end foreach
if(chkbx.Checked){
actSession acts = new
actSession(chkbx.ID.Substring(chkbx.ID.Length - 3));
sessionsTable.Add(chkbx.ID,acts);
TextBox tbx = (TextBox)PatternTable.FindControl(idpre +
"fromtime");
tbx.Text = acts.fromTime.ToShortTimeString();
tbx = (TextBox)PatternTable.FindControl(idpre +
"totime");
tbx.Text = acts.toTime.ToShortTimeString();
string jscrpt = null;
if(acts.fromTime.CompareTo(acts.toTime) == 0){
jscrpt = "<script language=\"JavaScript\">";
jscrpt += "alert(\"yo yo fred\")";
jscrpt += "</script>";
string scriptKey = "windowload:" + this.UniqueID;
//HERE IS THE REGISTERSTARTUPSCRIPT LINE
RegisterStartupScript(scriptKey,jscrpt);
}//end if
}//end if
else{
TextBox tbx = (TextBox)PatternTable.FindControl(idpre +
"fromtime");
tbx.Text = null;
tbx = (TextBox)PatternTable.FindControl(idpre +
"totime");
tbx.Text = null;
}//end else
Session["sessionsTable"] = sessionsTable;
Session["patternTable"] = PatternTable;
}//end
}
}
"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:uQ**************@tk2msftngp13.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_SPAM_4_MEyahoo.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Bill Jones wrote:
I'm trying to use this.RegisterStartupScript 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
"CheckChanged" 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 "CheckChanged" 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