Priscilla Jobin
Priscilla Jobin

Reputation: 607

alert shows undefined in javascript when passed value form aspx.cs page

 string locationName = "Mumbai";
    Page.ClientScript.RegisterStartupScript(Type.GetType
    ("System.String"), "addScript", "PassValues(" + locationName + ")", true);

in javascript my code contains

<script language="javascript" type="text/javascript">
        function PassValues(locationName)
            {
             var txtValue = locationName; 
             alert(txtValue);
            }
</script>

Here the alert shows undefined instead of "Mumbai"

Upvotes: 2

Views: 3545

Answers (5)

Davide Piras
Davide Piras

Reputation: 44605

this works perfectly for me:

Default.aspx.cs

using System;
using System.Web.UI;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string locationName = "Mumbai";

            Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "PassValues('" + locationName + "')", true);
        }
    }
}

Default.aspx ( auto generated as content page from Visual Studio 2010 when created the new web application for testing )

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<script language="javascript" type="text/javascript">
    function PassValues(locationName) {
        var txtValue = locationName;
        alert(txtValue);
    }
</script>

</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
</asp:Content>

Upvotes: 2

Abdul Munim
Abdul Munim

Reputation: 19217

You missed the quotation to pass as string in your PassValues javascript function

 string locationName = "Mumbai";
    Page.ClientScript.RegisterStartupScript(Type.GetType
    ("System.String"), "addScript", "PassValues('" + locationName + "')", true);

Upvotes: 1

Simon
Simon

Reputation: 2830

You need to quote the parameter

Change:

"PassValues(" + locationName + ")" 

To

"PassValues('" + locationName + "')"

Upvotes: 1

Saeed Neamati
Saeed Neamati

Reputation: 35842

Just to quickly remove your problem, you can simply use inline ASP.NET, to quickly run your application:

<script language="javascript" type="text/javascript">
        function PassValues(locationName)
        {
           var txtValue = locationName; 
           alert(txtValue);
        }
        PassValues('<%= locationName %>');
</script>

But, the problem is, your code is rendered in browser as:

PassValues(Mumbai);

This means that JavaScript tries to find a variable called Mumbai, and since it won't find it, the undefined message would be displayed. Therefore, you should correct your code as:

"PassValues('" + locationName + "')"

Upvotes: 1

Bert
Bert

Reputation: 82469

Try putting single quotes around your variable in the code behind. Without them, the browser thinks you are passing in a variable named Mumbai. What you really want to pass is the string 'Mumbai'. You get the message, 'undefined', because there is no variable named Mumbai in the client side code.

 string locationName = "Mumbai";
    Page.ClientScript.RegisterStartupScript(Type.GetType
    ("System.String"), "addScript", "PassValues('" + locationName + "')", true);

Upvotes: 4

Related Questions