Reputation: 607
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
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
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
Reputation: 2830
You need to quote the parameter
Change:
"PassValues(" + locationName + ")"
To
"PassValues('" + locationName + "')"
Upvotes: 1
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
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