Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi,

I have a javascriipt function in my .aspx page. How can I call this function from .cs page, so as that the function accepts a parameter that I send it from .cs page on page load? Please help me in this regard.

Thanks in advance.
Posted
Comments
Sunasara Imdadhusen 25-Oct-10 5:54am    
Which event you want to call like button click,change,checked..?
bedathur_ganesh 25-Oct-10 6:06am    
Let me make it clear..
I have a javascript function as below:

function Initialisation(fileName) {
var elem = document.getElementById("aaa");
elem.filename = '"+fileName+"';
}
I want to pass the "fileName" from .cs page to the javascript function on page load and I want ti assign the fileName value to elem.filename

Can you please guide me?

Hi,

You can call javascript function from server side using following function.

Updated:
your javascript function:
C#
function Initialisation(fileName) { var elem = document.getElementById("aaa"); elem.filename = '"+fileName+"'; }

your aspx.cs page:
C#
string fileName = "test.aspx";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ScriptRegistration", "Initialisation('"+ fileName +"');", true);




Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
v2
You can't call it from the .cs page, but you can pass the reference to the c# code which can then execute and pass back to the JS script.
 
Share this answer
 
Comments
bedathur_ganesh 25-Oct-10 6:06am    
Let me make it clear..
I have a javascript function as below:

function Initialisation(fileName) {
var elem = document.getElementById("aaa");
elem.filename = '"+fileName+"';
}
I want to pass the "fileName" from .cs page to the javascript function on page load and I want ti assign the fileName value to elem.filename

Can you please guide me?
Call below code snipet from any event like button click or page load or any other event
XML
string filename="assign value";

 System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script language='javascript'>");
        sb.Append(@"Initialisation('" + filename + "');");
        sb.Append(@"</script>");
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);
 
Share this answer
 
hi try like this
string fileName = "your filename here";

Page.ClientScript.RegisterStartupScript((Type.GetType(Initialisation(fileName)), "Initialisation(fileName)", "if u wantshow the value use ShowValue()", true);
i hope it will useful to u.


http://www.daniweb.com/web-development/aspnet/threads/368821/calling-javascript-function-in-asp.net-app-[^]


thanks and regards
nareshrajuu
 
Share this answer
 
v2

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