Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

From Code behind I want to call JavaScript function and pass the value
I have written a code in code behind as follows
Code Behind:

C#
Disp=Disp+"<img src="path"  önClick="fun_Call_JavaScript("+str_val+")">"

str_val is retrieved from query.

From Code behind I want to call JavaScript function and pass the value(str_val)

JavaScript
function fun_Call_JavaScript(a)
{
alert(a)
}


But it gives a error as undefined.
Kindly suggest, how to get the value.
Posted
Updated 14-Sep-12 3:39am
v2

Look at the standard way of doing this:
Register Client script in ASP.NET[^]
 
Share this answer
 
This should do the job:

Page.RegisterStartupScript("starScript", "fun_Call_JavaScript('" + a + "');");
 
Share this answer
 
Comments
Arunprasath Natarajan 14-Sep-12 10:01am    
If suppose a value is "I_Got_Value". The error message shows as

Webpage error details
Message: 'I_Got_Value' is undefined
BobJanova 14-Sep-12 11:55am    
Are you sure you included all the quotes in this example? Including those single ones in the string around a?
Arunprasath Natarajan 15-Sep-12 2:31am    
Yeah, Its working for int but not for string
using:
MSDN: RegisterClientScriptBlock Method[^] OR
MSDN: RegisterStartupScript Method[^]

Using them, you can inject the scripts into a page from server side and based on certain condition (and specific to page.)
For samples on how to use it: How to Use RegisterClientScriptBlock & RegisterStartupScript[^]
Inject javascript from server - Sample[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900