I want to call a function from my code behind using javascript. I used the below code:
function fnCheckSelection() {
some script;
window["My"]["Namespace"]["GetPart"](null);
}
...where "GetPart"
is the function name. However, this is not working. Please help me on this.
You can't call a Javascript function from the CodeBehind, because the CodeBehind file contains the code that executes server side on the web server. Javascript code executes in the web browser on the client side.
In order to call the JavaScript function with parameter from Code Behind, one has to make use of the RegisterStartupScript method of the ClientScript class in ASP.Net using C# and VB.Net. The following HTML Markup consists of an ASP.Net Button and a Label control.
in JavaScript:
document.getElementById("btnSample").click();
Server side control:
<asp:Button runat="server" ID="btnSample" ClientIDMode="Static" Text="" style="display:none;" OnClick="btnSample_Click" />
C#
protected void btnSample_Click(object sender, EventArgs e)
{
}
It is easy way though...
You can do this by an ajax call
this is a jquery example:
$.ajax({
type: "POST",
url:"~/code_behind.aspx/Method",
data: dataPost,
contentType: "application/json; charset=utf-8",
dataType: "json",
....
});
here is api documentation and in code behind
[WebMethod]
public static yourType Method (Params){}
or you can add a hidden button inside updatePanel, and invoke the click event using js. ('#<%=ID.ClientID%>').click();
It will invoke the OnClientClick if it exists then your codeBehind fucntion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With