473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Page.RegisterClientScriptBlock rebuilds Javescript for every control

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",Date PickerJs());
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 (so*******@hotmail.com) -->\n");
.....
Nov 17 '05 #1
2 1974
Earl Teigrob wrote:
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",Date PickerJs());
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 (so*******@hotmail.com) -->\n");
....


Sure:

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

Page.RegisterClientScriptBlock("DatePickerJs",_dat ePickerJs);

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.

--
mikeb

Nov 17 '05 #2
Great! Thanks for the advice. Worked Perfectly! I have not worked with
resource files but I will look into using them instead...

Earl

"mikeb" <ma************@mailnull.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Earl Teigrob wrote:
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",Date PickerJs());
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 (so*******@hotmail.com) -->\n"); ....


Sure:

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

Page.RegisterClientScriptBlock("DatePickerJs",_dat ePickerJs);

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.

--
mikeb

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steve McLellan | last post by:
Hi, We've got two developers working on the same mixed C++ project, using .SLN and .CPROJ project file (i.e. with identical configurations) on two different VS .NET 2003. The project files are...
1
by: John Livermore | last post by:
I have a user control that can be created from a variety of other user controls. It is the same no matter where it is created from, and I only want one instance of it on the page. Is there a...
3
by: Arun K | last post by:
Hi, I am creating a simple .aspx page to add some fields with validation. I have used different .NET validations like REquiredFieldValidator, RegularExpressionValidator and showed the summary...
2
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! When i develop my web application i have a set of javascript that is used in several pages. Also the title is the same for all pages. And certain controls have events that are in the...
9
by: Nathan Sokalski | last post by:
I have used the RegisterClientScriptBlock method in external functions of mine (ones that are saved in a separate *.vb file), where I use them as follows: Public Shared Sub MyFunction(ByVal...
4
by: Mori | last post by:
I am using masterPage and I need to populate a textbox that is in a content control with data from popup page that is not part of the master page. This code works if no masterpage is involved. ...
3
by: CodeRazor | last post by:
I have a user contol hosted in a webform. The user control interfaces with an http component. When the user control loads, a request is sent to the http component. It takes around 10 seconds...
2
by: CrystalMikeMD | last post by:
Greetings, I've been at this problem for some time now and have decided to seek out some help. Essentially, this is what I have. A basic ASP.NET 2.0 page. On this page is the standard...
2
by: bob | last post by:
Hi, I have a page that has a collection of things which it passes to a second 'print' page using a session variable. The print page has buttons for moving though the collection and the user ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.