Mixing javascript and ASP.net code

Discussion in 'ASP .Net' started by Aussie Rules, Jun 14, 2007.

  1. Aussie Rules

    Aussie Rules Guest

    Hi.

    I am using Live Earth SDK, and have very little Javascript knowledge, but I
    need to insert values into a Javascript function, where the vales are in the
    ASP.net VB code.

    The javascript function is below, but I need to replace the value
    (47.6, -122.33) with variables in the code behind page that I get from my
    SQL 2000 database.

    Is there a way to do this ?


    Thanks


    function GetMap()
    {
    map = new VEMap('myMap');
    map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
    }
     
    Aussie Rules, Jun 14, 2007
    #1
    1. Advertising

  2. Hi Aussie,

    I hope you are well. There are two ways of doing it.
    1. Use methods offered by ClientScriptManager class
    i.e.

    protected void Page_Load(object sender, EventArgs e)
    {

    // these variables should be set to values
    // retreived from database
    double x = 47.6;
    double y = -122.33;

    if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "mapScript"))
    {

    string script =
    "<script type=\"text/javascript\">\n" +
    "function GetMap() {{\n" +
    " var map = new VEMap('myMap');\n" +
    " map.LoadMap(new VELatLong({0}, {1}), 10 ,'h' ,false);\n" +
    "}}\n" +
    "</script>";

    ClientScript.RegisterClientScriptBlock(
    this.GetType(),
    "mapScript",
    String.Format(script, x, y));
    }
    }


    or storing retreived values in properties and then using <%%> code block on
    the aspx page:


    -- code behind/beside --

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    // populate with values from database
    XCoordinate = 47.6;
    YCoordinate = -122.33;
    }
    }

    // i used viewstate in order to
    // not to make database roundtrips on postbacks
    protected double XCoordinate
    {
    get
    {
    object value = ViewState["XCoordinate"];
    return value == null ? 0.0d : (double)value;
    }
    set
    {
    ViewState["XCoordinate"] = value;
    }
    }

    protected double YCoordinate
    {
    get
    {
    object value = ViewState["YCoordinate"];
    return value == null ? 0.0d : (double)value;
    }
    set
    {
    ViewState["YCoordinate"] = value;
    }
    }

    -- end code behind/beside --


    -- aspx page code --

    <script type="text/javascript">

    function GetMap()
    {
    map = new VEMap('myMap');
    map.LoadMap(new VELatLong(<%= XCoordinate %>, <%= YCoordinate %>), 10 ,'h'
    ,false);
    }

    </script>
    -- end aspx page code --


    Hope it helps

    --
    Milosz


    "Aussie Rules" wrote:

    > Hi.
    >
    > I am using Live Earth SDK, and have very little Javascript knowledge, but I
    > need to insert values into a Javascript function, where the vales are in the
    > ASP.net VB code.
    >
    > The javascript function is below, but I need to replace the value
    > (47.6, -122.33) with variables in the code behind page that I get from my
    > SQL 2000 database.
    >
    > Is there a way to do this ?
    >
    >
    > Thanks
    >
    >
    > function GetMap()
    > {
    > map = new VEMap('myMap');
    > map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
    > }
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 15, 2007
    #2
    1. Advertising

  3. Aussie Rules wrote:
    > Hi.
    >
    > I am using Live Earth SDK, and have very little Javascript knowledge,
    > but I need to insert values into a Javascript function, where the vales
    > are in the ASP.net VB code.
    >
    > The javascript function is below, but I need to replace the value (47.6,
    > -122.33) with variables in the code behind page that I get from my SQL
    > 2000 database.
    >
    > Is there a way to do this ?
    >
    >
    > Thanks
    >
    >
    > function GetMap()
    > {
    > map = new VEMap('myMap');
    > map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
    > }
    >


    Put a Literal control where you want the value, and set the Text
    property of the control in code behind.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 15, 2007
    #3
  4. Aussie Rules

    bruce barker Guest

    function GetMap()
    {
    map = new VEMap('myMap');
    map.LoadMap(new VELatLong(<%=codebehindfunc1()%>,
    <%=codebehindfunc2()%>),
    10 ,'h' ,false);
    }

    -- bruce (sqlwork.com)


    Aussie Rules wrote:
    > Hi.
    >
    > I am using Live Earth SDK, and have very little Javascript knowledge,
    > but I need to insert values into a Javascript function, where the vales
    > are in the ASP.net VB code.
    >
    > The javascript function is below, but I need to replace the value (47.6,
    > -122.33) with variables in the code behind page that I get from my SQL
    > 2000 database.
    >
    > Is there a way to do this ?
    >
    >
    > Thanks
    >
    >
    > function GetMap()
    > {
    > map = new VEMap('myMap');
    > map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
    > }
    >
     
    bruce barker, Jun 15, 2007
    #4
    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. Jamie
    Replies:
    1
    Views:
    752
    Jamie
    Dec 5, 2003
  2. Mario Rodriguez

    mixing vbscript and ASP.NET

    Mario Rodriguez, Apr 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,557
    mcanty
    Dec 17, 2008
  3. Dan

    Mixing ASP with ASP.net

    Dan, Feb 15, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    4,572
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Feb 15, 2006
  4. =?Utf-8?B?TWlrZQ==?=

    Mixing Non-ASP.NET & ASP.NET Files in One Domain

    =?Utf-8?B?TWlrZQ==?=, Aug 10, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    301
    Cowboy \(Gregory A. Beamer\)
    Aug 10, 2007
  5. Mario Rodriguez

    mixing vbscript and ASP.NET

    Mario Rodriguez, Apr 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    116
    Mario Rodriguez
    Apr 21, 2004
Loading...

Share This Page