You can't do things like this; that is, you can't create a control tree and stick it into the
session and then add it back in on postback -- you'll have to recreate the table each time. I'm not
sure how you were able to see the postback event on the checkbox fire.... hummmm.....
Here's a sample that does something similar to what you have (you should look into composite
controls if you choose do things this way).
Scott
<%@ Page language="c#" AutoEventWireup="false" trace="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>LblCssClass</title>
<script language="C#" runat="server">
private void Page_Load(object sender, EventArgs e)
{
this.EnsureChildControls();
}
protected override void CreateChildControls()
{
Table table = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += new EventHandler(CheckBox_Changed);
td.Controls.Add(cb);
tr.Cells.Add(td);
table.Rows.Add(tr);
this.Form1.Controls.Add(table);
}
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
}
protected void CheckBox_Changed(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
this.Lbl.Text = "Changed " + cb.Checked;
AddJScript();
}
private void AddJScript()
{
string jscrpt = "<script language=\"JavaScript\">";
jscrpt += "alert(\"yo yo fred\");";
jscrpt += "</";
jscrpt += "script>";
string scriptKey = "windowload:" + this.UniqueID;
RegisterStartupScript(scriptKey,jscrpt);
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>
<asp:label id="Lbl" runat="server">Hello</asp:label></p>
</form>
</body>
</html>
Bill Jones said:
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
}
}