RegisterStartupScript Question.

B

Bill Jones

I'm trying to use this.RegisterStartupScript to add some javascript to and
aspx page that will run when the page is loaded. Does anyone know if this
function 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 javascript
code 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 checkboxes
I need to determine if two values match each other. If they do, then I want
to use some javascript to open a modal dialog to allow for the input of two
new values. Make sense? Any help would be great! Thanks.

- Bill Jones
 
R

Raymond Lewallen

Bill,

Is your checkbox control set to runat=server? Is the AutoPostBack=True?

You can put this anywhere. In your CheckChanged event sub, you can put:

Dim sb As New System.Text.StringBuilder
sb.Append("<script language='javascript'>")
sb.Append("window.open('mypage.aspx','help','width=400,height=400,left=100,t
op=100,toolbars=no,status=no,scrollbars=yes,resizable=yes');")
sb.Append("</script>")
RegisterStartupScript("stp", sb.ToString)

or whatever your code is your using to open a modal dialog, the above opens
a window, but its just an example

HTH,

Raymond Lewallen
 
C

Craig Deelsnyder

Bill said:
I'm trying to use this.RegisterStartupScript to add some javascript to and
aspx page that will run when the page is loaded. Does anyone know if this
function 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 javascript
code 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 checkboxes
I need to determine if two values match each other. If they do, then I want
to use some javascript to open a modal dialog to allow for the input of two
new 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?
 
S

Scott

One place you shouldn't call them is Render; as a rule I always call them in PreRender.

Scott
 
C

Craig Deelsnyder

Scott said:
One place you shouldn't call them is Render; as a rule I always call them in PreRender.

Scott


Bill Jones wrote:



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?
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...
 
B

Bill Jones

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
}
}




 
S

Scott

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
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top