Page.RegisterClientScriptBlock rebuilds Javescript for every control

E

Earl Teigrob

I have a Web Custom Control that builds javascript to write to the page.
The DatePickerJs() function builds over 500 lines of javacript code. Even
thought my Custom Web Control does not write it to the client, it
regenerates the string very time this control appears on a page.

Is there a way to create this string ONCE and then just use if every time
after that?


CODE
protected override void CreateChildControls()
{
Page.RegisterClientScriptBlock("DatePickerJs",DatePickerJs());
TextBox t = new TextBox();
t.Text="TextBox Text";
t.Width=System.Web.UI.WebControls.Unit.Pixel(80);
this.Controls.Add(t);
this.Controls.Add(new LiteralControl("&nbsp<a
href=\"javascript:show_calendar('forms[0]." + t.ClientID + "');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\" TABINDEX=\"25\"><img
src=\"http://" + Page.Request.ServerVariables["SERVER_NAME"]+
Page.Request.ApplicationPath + "/CustomResources/show-calendar.gif\"
width=\"24\" height=\"22\" border=\"0\"></a>"));
}


private string DatePickerJs()
{
StringBuilder s = new StringBuilder(600);
s.Append("<script language=\"JavaScript\">\n");
s.Append("<!-- Original: Kedar R. Bhave ([email protected]) -->\n");
.....
 
M

mikeb

Earl said:
I have a Web Custom Control that builds javascript to write to the page.
The DatePickerJs() function builds over 500 lines of javacript code. Even
thought my Custom Web Control does not write it to the client, it
regenerates the string very time this control appears on a page.

Is there a way to create this string ONCE and then just use if every time
after that?


CODE
protected override void CreateChildControls()
{
Page.RegisterClientScriptBlock("DatePickerJs",DatePickerJs());
TextBox t = new TextBox();
t.Text="TextBox Text";
t.Width=System.Web.UI.WebControls.Unit.Pixel(80);
this.Controls.Add(t);
this.Controls.Add(new LiteralControl("&nbsp<a
href=\"javascript:show_calendar('forms[0]." + t.ClientID + "');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\" TABINDEX=\"25\"><img
src=\"http://" + Page.Request.ServerVariables["SERVER_NAME"]+
Page.Request.ApplicationPath + "/CustomResources/show-calendar.gif\"
width=\"24\" height=\"22\" border=\"0\"></a>"));
}


private string DatePickerJs()
{
StringBuilder s = new StringBuilder(600);
s.Append("<script language=\"JavaScript\">\n");
s.Append("<!-- Original: Kedar R. Bhave ([email protected]) -->\n");
....

Sure:

1) Change the RegisterClientScriptBlock() call to look like:

Page.RegisterClientScriptBlock("DatePickerJs",_datePickerJs);

2) Change DatePickerJs() to be a static method.

3) Add this to your web custom control class:

static private string _datePickerJs = DatePickerJs();


That's a low-impact change from what you have now. However a better way
(in my opinion) would be to place your script in a file, and add that
file to the project as a resource - then you can pull the script in as
string from the assembly's resources.

I'd think it would be easier to maintain a 500 line script which lives
in its own file than 500 lines of s.Append() calls.

One thing to be aware of if you go this way is that if you change the
embedded file resource, VS.NET has a bug such that it requires a
*rebuild all* to incorporate the updated file into the resources - a
simple build doesn't do it.
 
E

Earl Teigrob

Great! Thanks for the advice. Worked Perfectly! I have not worked with
resource files but I will look into using them instead...

Earl

mikeb said:
Earl said:
I have a Web Custom Control that builds javascript to write to the page.
The DatePickerJs() function builds over 500 lines of javacript code. Even
thought my Custom Web Control does not write it to the client, it
regenerates the string very time this control appears on a page.

Is there a way to create this string ONCE and then just use if every time
after that?


CODE
protected override void CreateChildControls()
{
Page.RegisterClientScriptBlock("DatePickerJs",DatePickerJs());
TextBox t = new TextBox();
t.Text="TextBox Text";
t.Width=System.Web.UI.WebControls.Unit.Pixel(80);
this.Controls.Add(t);
this.Controls.Add(new LiteralControl("&nbsp<a
href=\"javascript:show_calendar('forms[0]." + t.ClientID + "');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\" TABINDEX=\"25\"><img
src=\"http://" + Page.Request.ServerVariables["SERVER_NAME"]+
Page.Request.ApplicationPath + "/CustomResources/show-calendar.gif\"
width=\"24\" height=\"22\" border=\"0\"></a>"));
}


private string DatePickerJs()
{
StringBuilder s = new StringBuilder(600);
s.Append("<script language=\"JavaScript\">\n");
s.Append("<!-- Original: Kedar R. Bhave ([email protected]) -->\n");
....

Sure:

1) Change the RegisterClientScriptBlock() call to look like:

Page.RegisterClientScriptBlock("DatePickerJs",_datePickerJs);

2) Change DatePickerJs() to be a static method.

3) Add this to your web custom control class:

static private string _datePickerJs = DatePickerJs();


That's a low-impact change from what you have now. However a better way
(in my opinion) would be to place your script in a file, and add that
file to the project as a resource - then you can pull the script in as
string from the assembly's resources.

I'd think it would be easier to maintain a 500 line script which lives
in its own file than 500 lines of s.Append() calls.

One thing to be aware of if you go this way is that if you change the
embedded file resource, VS.NET has a bug such that it requires a
*rebuild all* to incorporate the updated file into the resources - a
simple build doesn't do it.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top