migmig
migmig

Reputation: 141

How to set a img in my aspx page through javascript?

Maybe it sounds stupid, but I really can't figure out how to set an <img> code in my .aspx page throught JavaScript.

My code so far:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Teste.aspx.vb"  Inherits="Teste" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

Latitude: <asp:TextBox ID="nr_latitudeTextBox" runat="server" MaxLength="12" /> <br />
Longitude: <asp:TextBox ID="nr_longitudeTextBox" runat="server" MaxLength="12" /><br />

<asp:Button ID="cmb_mapa" runat="server" Text="Mapa" OnClick="cmb_mapa_Click"/><br />
        <script language="javascript" type="text/javascript">

                function fonte(lat, lon) {
                    var src = "http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false";
                    testando("http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false", 540, 280, "Mapa");
                }

                function testando(src, width, height, alt) {
                    var img = document.createElement("img");
                    img.src = src;
                    img.width = width;
                    img.height = height;
                    img.alt = alt;
                    document.body.appendChild(img);
                }
        </script>

    <div id="img">

    </div>

</asp:Content>

Partial class:

Partial Class Teste
    Inherits System.Web.UI.Page


    Protected Sub cmb_mapa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_mapa.Click
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "testando", "testando(" & nr_latitudeTextBox.Text & "," & nr_longitudeTextBox.Text & ");", True)
    End Sub
End Class

Upvotes: 1

Views: 189

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68440

testando first parameter is the image url and you're not providing it.

It seems you want to call fonte and not testando on your RegisterStartupScript. That's the method that takes latitude and longitude as parameters.

EDIT

Change

var src = "http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&ma‌​ptype=roadmap&sensor=false"
testando("http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false", 540, 280, "Mapa");

by

var src = "http://maps.google.com/maps/api/staticmap?center=" + lat + "," + lon + "&zoom=8&size=540x280&ma‌​ptype=roadmap&sensor=false";
testando(src, 540, 280, "Mapa");

Fixed two things:

1) You weren't using lat and lon as parameter for the map url so fixed src building

2) Added src as parameter for testeando since you weren't using it

Upvotes: 2

Related Questions