So my question is, is there a more elegant way of doing this? If not, is
there some way that I could write something that would scan the static JS
that I've put on any page before rendering that page and replace control
references with Control.ClientID or something like that?
Alex
"Alex Maghen" <AlexM...@newsgroup.nospam> wrote in message
news:259BC30D-12C5-4E13...@microsoft.com...
Hi Alex,
I think Nathan's suggestion on creating some script template files and load
script fragment from them at runtime is a good idea. Also, more
specifically, you can consider the following means:
1). create a separate class library project
2) create some script template files(text files) which contains the scritp
functions/template functions and compiled them as "embeded resources" in
that class library project.
3). Reference that class library in your ASP.NET web application so that
you can dynamically load the script/template files as resource stream from
the class library assembly. e.g.
===============
protected void Page_Load(object sender, EventArgs e)
{
Stream stream =
typeof(WSProxyLib.Class1).Assembly.GetManifestResourceStream("WSProxyLib.scr
ipttemplate.txt");
StreamReader sr = new StreamReader(stream);
string template = sr.ReadToEnd();
Response.Write("<br/>" + Server.HtmlEncode(template));
}
===============
Also, you can use the "WebResource" feature to link external files(css or
script files that embeded in .net assembly) in your asp.net page
#WebResource ASP.NET 2.0 explained
http://www.codeproject.com/aspnet/MyWebResourceProj.asp
In addition, if you do want to put the script functions in aspx template
and utilize the server control's "ClientID" property, you can consider
using the <%= %> expression to embed ClientID into script. e.g.
================
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" >
function testfunc()
{
var id = "<%= Button1.ClientID %>";
var elm = document.getElementById( id );
alert(elm.value);
}
</script>
</head>
================
You can tried this in your aspx page if there is just some simple script
snippets. However, we still recommend that you consider using a template
file and load it at runtime because embed <%= %> will make the code logic
mixed with UI template in aspx which is not good practice generaly.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial
response from the community or a Microsoft Support Engineer within 1
business day is
acceptable. Please note that each follow up response may take approximately
2 business days
as the support professional working with you may need further investigation
to reach the
most efficient resolution. The offering is not appropriate for situations
that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis
issues. Issues of this nature are best handled working with a dedicated
Microsoft Support
Engineer by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
If I actually type client-side JS into my ASPX file in a <SCRIPT> tag, can I
access and re-write the full text content of that <SCRIPT> tag on PageLoad or
Render or something? Ideally, I'd like to write a function that steps through
all of the client-side <SCRIPT> tags already embedded in the ASPX page and
cleans them up before the page is delivered.
Can the Page.ClientScript object give me access to a collection of already
existent client-side scripts on the page? If not, is there another way that I
could iterate through any client-scripts already on the page and edit them
pre-delivery?
Alex
"Alex Maghen" <AlexM...@newsgroup.nospam> wrote in message
news:DB1B1DE5-5715-4E44...@microsoft.com...
Hi Alex,
Theoretically speaking, you can control the content in the client <script>
...</script> block in the aspx template by the following means:
1. still use the <%= PageVariable %> render expression to inject text
content into aspx output. e.g.
=====in aspx==========
<head runat="server">
.....................
<script id="script2" language="javascript" type="text/javascript">
<%= InlineScript %>
</script>
....................
</head>
................................
========in page code=========
public partial class ASPNETPage : System.Web.UI.Page
{
protected string InlineScript;
protected void Page_Load(object sender, EventArgs e)
{
InlineScript =
@"function TestFun2()
{
alert('test function2!');
}
";
}
}
=============================
2. put an ASP.NET Literal control in the <script > block and assign the
script functions to the Literal control's "Text" property in code behind.
e.g
======in aspx=========
<head runat="server">
.....................................
<script id="script1" language="javascript" type="text/javascript">
<asp:Literal id="ltScript" runat="server" ></asp:Literal>
</script>
..................
</head>
==========in code behind=============
public partial class ASPNETPage : System.Web.UI.Page
{
protected string InlineScript;
protected void Page_Load(object sender, EventArgs e)
{
ltScript.Text =
@"function TestFun()
{
alert('test function!');
}
";
}
}
=========================
However, the above means is not recommended, we would prefer using the
Page.ClientScript.XXX method for registering startup script or script
block. These functions will ensure the scritp block be inserted in the
proper location in the page output.
Hope this helps further.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead