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"
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"
Share Improve this question edited Nov 16, 2011 at 9:04 Termi 4456 silver badges19 bronze badges asked Nov 16, 2011 at 9:00 Priscilla JobinPriscilla Jobin 6077 silver badges20 bronze badges 1- Wele to SO, please use {} tool box button to mention your code parts. – Sai Kalyan Kumar Akshinthala Commented Nov 16, 2011 at 9:02
5 Answers
Reset to default 4Try 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);
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>
Wele to ASP.NET!
</h2>
</asp:Content>
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 + "')"
You need to quote the parameter
Change:
"PassValues(" + locationName + ")"
To
"PassValues('" + locationName + "')"
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);