C# .NET - javascript / c#
Asked By Craig Vedur
20-Jun-05 05:04 PM
hi again,
I was trying to implement some functionality that i read about here. i was having a problem with my usercontrols... being that since i used the same controls on different forms, none of my JS would work due to the naming containers.
so, i was told to generate the JS dynamically within c# and use RegisterStartupScript
however, seems to me that this will make maintenance of the code difficult b/c it'll be hard to read and will be embedded within my C# code.
is there another approach.... by leaving the JS code external and doing some replacement variables or something?
You could either embed the script in the assembly
as an embedded resource and pull it out with GetManifestResource, or you could simply RegisterStartupScript with something like so:
<script src="myscriptname.js"></script>
You can also create a user control that
creates your script. You'll have to pass in the appropriate variable for the client ids. This way you can separate the script from your C# code.
I generally create constant format strings for my client code adn use String.Format to emit the client script. This also gives you some degree of separation while still allowing you to see the dependency.
Downside and another approach

You are going to have a couple of downsides either way you do it.
If you have a large amount of javascript then it's often better to reference it using the "src" attribute of the script tag and place it in the path. This allows the web browser to download it once and cache it in subsequent requests.
If you have a small amount of javascript then embedding it doesn't make much of a difference. It's quick and easy to make it work with your controls or whatever you want. You end up sending the same script down every time the page loads but it really doesn't matter.
I have found it easier for someone having your troubles to just embed the script and move on to more pressing issues so that is why I suggested the solution to you. You were having trouble understanding how to get the javascript to work within the naming containers and you didn't have much javascript.
Now that you have the code working, you understand the problems, you have thought of better ways but you need some help implementing it. I hope that about sums it up.
Creating embedded resources from stand-alone Javascript files is what I'm going to describe. I'm going to show you how to have your javascript in a separate standalone .js file within the ide and embed it as a resource in the dll.
Create a folder in your project for your javascript files and name it JScript. Something like this...
<Web Project>
JScript
MyPage.js
MyControls.js
The .js files need to be compiled as embedded resources within Visual Studio so select the .js files and set their build action to "Embedded Resource" in the Properties window.
Now you need to get the javascript from the dll as a string since it is an embedded resource and that can be done with the following code.
string GetResourceString(string aScript)
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream str = executingAssembly.GetManifestResourceStream(aScript);
using(StreamReader sr = new StreamReader(str, Encoding.ASCII))
{
return sr.ReadToEnd();
}
}
Within your page you can continue to register your javascript as normal except that you will be calling the GetResourceString method above to load your javascript.
if(!Page.IsClientScriptBlockRegistered("MyPage"))
{
string sScript = ScriptHelper.GetResourceString("JScript.MyPage.js");
Page.RegisterClientScriptBlock("MyPage", sScript);
}

FOLLOW THIS -http: / / jqueryui.com / demos / dialog / HI 1. Open a New Web project in Visual Studio 2. Create two New WebForm pages named ParentWebForm and ChildWebForm 3. Open the HTML surface and that the script isn't allready on the Page if (page ! = null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) { page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script); } } } Popup windows are new browser windows opened
Text+ = "<BR / > " +Request.Form[ "txtLayer" ].ToString(); } / / set up client script to handle modalDialog if (!Page.IsClientScriptBlockRegistered( "Dlg" )) { string title = " Test Title" ; string userId = "123" ; string layer = "2" ; string parameters = "&userId = " +userId has a complete implementation you can use "out of the box". Enjoy. Download the working Visual Studio 2010 Demo Solution Popularity ( 8486 Views ) View Peter Bromberg's Articles Picture Biography - Peter Bromberg
_amInQuote = false; } } } else { _amInQuote = true; } } / / if current char is quote } C# Discussions Windows XP (1) Visual Studio (1) RoutedEventArgs (1) StringComparison (1) DirectoryInfo (1) StringBuilder locations of comments (;) and quotes (") that i add the style rule once, since it is nathansokalski.com / ASP.NET Web Controls Discussions IsClientScriptBlockRegistered (1) RegisterClientScriptBlock (1) CreateStyleRule (1) HtmlLink (1) Header (1) Contains (1) Stylesheet (1) Njsokalski (1
algo se me est = E1 escapando. = BFAlguna sugerencia? ASP.NET - Spanish Discussions ClientScript.RegisterStartupScript (1) IsClientScriptBlockRegistered (1) Visual Studio (1) HttpWebResponse (1) HttpWebRequest (1) HttpStatusCode (1) RegisterStartupScript (1) Response.ContentType (1) ?C?mo has configurado los permisos? En el entorno de desarrollo, usando el servidor interno de Visual Studio, el code-behind de la p?gina se ejecuta con los permisos del usuario que est? ejecutando el Visual Studio. Este usuario eres t? mismo y probablemente tienes permisos para acceder a la carpeta compartida abreVentana(ByVal ventana As String) Dim scriptString As String = "window.open('" + ventana + If (Not ClientScript.IsClientScriptBlockRegistered("WOpen")) Then ClientScript.RegisterStartupScript(GetType(String), "WOpen", scriptString) End If End Sub Protected Function File_Exist_Server