How to Call Code Behind Server Method from a Client Side JavaScript Function

Server Side Function Calling Client Side Javascript Function For Results

You might have to use AJAX. Have your server code call the popup and have the popup call another method in the server side to process when the user submits. AJAX can do the client call to the server side. so your initial method would just bring up the popup and AJAX would call another method on the server to handle the submit.

Call Code Behind Function from JavaScript (not AJAX!)

Here's how I did it...

<script type="text/javascript">
function AddParticipant(companyid, employeenumber)
{
$("#AddParticipantPanel").dialog("close");
var myargs = {CompanyId: companyid, EmployeeNumber: employeenumber};
var myargs_json = JSON.stringify(myargs);
__doPostBack('<%= SelectParticipantBtn.UniqueID %>', myargs_json);
}
</script>
<asp:Button runat="server" ID="SelectParticipantBtn" ClientIDMode="Static" OnClick="SelectParticipantBtn_Click" style="display:none;" />

And on the server side...

/* this is an inner class */
public class SelectParticipantEventArgs
{
public string CompanyId {get; set;}
public string EmployeeNumber {get; set;}
}
protected void SelectParticipantBtn_Click(object sender, EventArgs e)
{
string args = Request.Form["__EVENTARGUMENT"];
var myargs = JsonConvert.DeserializeObject<SelectParticipantEventArgs>(args);
string emp_no = myargs.EmployeeNumber;
string company_id = myargs.CompanyId;
//do stuff with my arguments
}

Instead of using HiddenFields, I used a hidden button. This model works better because I can go directly to the button's event handler instead of having to add code to the Page_Load function.

How to call a codebehind function from javascript in asp.net?

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...

Calling JavaScript Function From CodeBehind

You may try this :

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);


Related Topics



Leave a reply



Submit