Using Control Properties in Embedded JavaScript Resource file

H

hungrymind

Hi

I am trying to develop asp.net server control which has several
property in the control and has embedded javascript files, which is
used by control via webresource from the client page. I want to use
some of the property defined in controls main class inside the
javascript. at the same time I dont want to mess C# code and wants to
keep JS file seperate & clean. Is there anyway to access C# property
of control inside the javascript embeded file, like using <%=
PropertyName %>.

It will be something like this

document.getElementById('<%= this.ClientID%>')

I tried this, though I had doubt, and it did not worked. Can anyone
suggest the best way to achieve this, as well as keeping my C# &
javascript code in seperatly.

wishing in anticipation

hungryMind
www.hungrymind-concepts.com
 
D

digitaljeebus

There's absolutely no way to modify that jscript file before it gets
to the client, short of wrapping it in a generic handler, reading the
file, and writing it out while replacing chunks of code. I've found a
couple of ways around this. One would be to add a line in your OnLoad
event handler such as the following:

this.ClientScript.RegisterClientScriptBlock(this.GetType(),
"someuniquekey", string.Format("var temp =
document.getElementById('{0}');", this.ctrl.ClientId));

Another way (much cleaner way) would be to create a javascript class
using the asp.net ajax clientscript library. Here's a tutorial:

http://asp.net/ajax/documentation/live/tutorials/CreatingCustomClientScripts.aspx

Just a couple of options, none of them great =) Good luck!
 
H

hungrymind

thanks digital jee,

however i tried to initialize all c# property to javascript using
relection like this

Type type = this.GetType();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("var Map"+this.ID+" = new MyMap();");
foreach (PropertyInfo propertyInfo in
type.GetProperties())
{
if (propertyInfo.PropertyType == typeof(string))
{
stringBuilder.Append(" Map" + this.ID + "." +
propertyInfo.Name + " = '" + propertyInfo.GetValue(this, null) +
"';");
}
if (propertyInfo.PropertyType == typeof(double))
{
stringBuilder.Append(" Map" + this.ID + "." +
propertyInfo.Name + " = " + propertyInfo.GetValue(this, null) + ";");
}
}

return stringBuilder.ToString();

this has resolved my issue so far, but I wanted to keep JS & C#
absolutely seperate. I will look into second options, if it is not
only related with AJAX then it will work for me.

many thanks

hungrymind
www.hungrymind-concepts.com
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top