Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Using Control Properties in Embedded JavaScript Resource file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hungrymind - 15 Mar 2008 00:27 GMT
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
digitaljeebus@gmail.com - 15 Mar 2008 06:51 GMT
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!
hungrymind - 15 Mar 2008 20:13 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.