dba123:
I'm new to writing custom controls. I have MyCustomControl.cs and in my Render method I want to render out about 50 lines of JavaScript. What's the best way to do this, use the writer?
protected override void Render(HtmlTextWriter writer)
{
writer.Write(@"
function decode(s)
{
return s.replace(/&/g, ""&"")
.replace(/"/g, '""')
.replace(/'/g, ""'"")
.replace(/</g, ""<"")
.replace(/>/g, "">"");
};"
);
I plan on having around 6 more writer.Write to write out some more sections here. Is that the best approach to actually perform the writing of JavaScript in this manor?
or should I use ClientScript.RegisterClientScriptBlock?
Hi,
If you are developing custom control and you want to use JavaScrip code. And it should be easily maintainable and readable for developer
Then you can write your JavaScript code in separate .js file. and use this JavaScript file as a embedded resource in your custom control dll.
So that your javascript code will be easily maintanable, readable as well as there is no deployment issue...because the .js fiel is embadded in the
custom Controls dll.
For this method
1) create one javascript file in the custom Control project
2) Add one namespace level attribut in your code
[assembly: WebResource("namespace.className.AsynchronousRequestManager.js", "text/javascript")]
3) And include this javascript code in your custom control
if (!Page.ClientScript.IsClientScriptIncludeRegistered(GetType(), "MyPortal_AsynchronousRequestManager"))
Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyPortal_AsynchronousRequestManager", Page.ClientScript.GetWebResourceUrl(GetType(), "namespace.classname.AsynchronousRequestManager.js"));
If you you want some more information , then search as 'Embadded Web resource in .net
Please mark this post as answer, if it helped