Using Control Properties in Embedded JavaScript Resource file

Discussion in 'ASP .Net' started by hungrymind, Mar 14, 2008.

  1. hungrymind

    hungrymind Guest

    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
    hungrymind, Mar 14, 2008
    #1
    1. Advertising

  2. hungrymind

    Guest

    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!
    , Mar 15, 2008
    #2
    1. Advertising

  3. hungrymind

    hungrymind Guest

    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
    hungrymind, Mar 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LidorP
    Replies:
    1
    Views:
    2,131
    Scott M.
    Nov 22, 2003
  2. Dirc Khan-Evans
    Replies:
    1
    Views:
    891
    Karl Seguin
    Oct 17, 2005
  3. Jay Pondy
    Replies:
    1
    Views:
    4,355
    Jay Pondy
    Feb 16, 2009
  4. Chuck P

    composite control embedded resource at designtime

    Chuck P, Sep 24, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    881
    Walter Wang [MSFT]
    Sep 25, 2007
  5. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    394
    Thomas Dodds
    Jul 26, 2004
Loading...

Share This Page