Click here to Skip to main content
15,902,737 members
Please Sign up or sign in to vote.
2.25/5 (3 votes)
See more:
How to call c# function by java script......plz reply me...
Posted
Updated 14-Aug-13 20:24pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Aug-13 8:27am    
There is no such thing as "Java script". Java is not a scripting language ;-)
—SA

Hey dude,Kindly visit the following links.. Hope that will help you solve your problem.

calling code behind function from javascript
[^]

Calling a code-behind function from JavaScript[^]
 
Share this answer
 
v2
Comments
VICK 15-Aug-13 2:43am    
Downvoted ??? The person who downvoted... can you please like to elaborate y??? So that I could improve my answer/knowledge!!!!
You may try this :
C#
Page.ClientScript.RegisterStartupScript(this.GetType(),"Call my function","MyFunction()",true);

for more Details visit here....
http://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind[^]
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/[^]
 
Share this answer
 
v4
Comments
VICK 15-Aug-13 2:01am    
He is asking the reverse...He wana call ASP function from JavaScript .... and not Javascript function from code behind...
It's quite straightforward using the __doPostBack call from Javascript.

In the Page_Load function in your code behind, put:

HTML
protected void Page_Load(object sender, EventArgs e)
{
    // ensure page always includes the __doPostBack function
// (best to put this in your Master Page, if you have one)
    Page.ClientScript.GetPostBackEventReference(this, string.Empty);

// now test if a postback event occurred
if (Page.IsPostBack)
{
            // postback occurred, so determine which function name was called
            switch (Request.Form["__EVENTTARGET"])
            {
                case "myProcedure1":
                    {
                        // go and call your function
                        doMyProcedure1(Request.Form["__EVENTARGUMENT"]);

                        break;
                    }
    }
}
}


Then further down in your code behind, create the C# function you wish to call:

HTML
protected void doMyProcedure1(String someVar)
{
// do your function code here
String passedVar = someVar;
}


Then finally in your Javascript, do the postback that calls the C# function:

JavaScript
function callMyFunction()
{
__doPostBack('myProcedure1', 'some string');
}
 
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