I have a function on my server side.
protected void SelectParticipant(string CompanyId, string EmployeeNumber)
{
//I do some stuff here.
}
I want to call this function from JavaScript on the client side. I've tried searching for it, but all the suggestions I found were for calling a Page Method via AJAX. I don't want that, I want to cause a postback.
function MyJSFunction(companyid, employeenumber)
{
//cause postback and pass the companyid and employee number
//this is where I need help!
}
How can I cause a postback and run a server side function from my JavaScript?
You can solve your problem using two HiddenField
.
- Set the two HiddenField using js/jquery(as you prefer)
- Force the form submit in the js function(
form1.submit()
) - In the formLoad check if the HiddenFields are empty. If no run your method and then clear the HiddenFields
You can use the __doPostBack function to post to server. just make sure you add a server control to the page so the function is inserted. here is a small example that you can customize for your needs:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Try1.WebForm1" EnableEventValidation="false"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#lblInvoke').hover(
function () {
__doPostBack('<%= LinkButton1.ClientID %>', 'value1,value2');
}
);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lblInvoke">Hover This!</label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Style="display:none;">LinkButton</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Try1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string passedArgument = Request.Params.Get("__EVENTARGUMENT");
string[] paramsArray = passedArgument.Split(',');
Label1.Text = string.Format("Returned from server. params: {0},{1}", paramsArray[0], paramsArray[1]);
}
}
}
Make sure you add the EnableEventValidation="false" attribute to page.
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"];
SelectParticipantEventArgs 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.
I have a function on my server side.
protected void SelectParticipant(string CompanyId, string EmployeeNumber)
{
//I do some stuff here.
}
I want to call this function from JavaScript on the client side. I've tried searching for it, but all the suggestions I found were for calling a Page Method via AJAX. I don't want that, I want to cause a postback.
function MyJSFunction(companyid, employeenumber)
{
//cause postback and pass the companyid and employee number
//this is where I need help!
}
How can I cause a postback and run a server side function from my JavaScript?
You can solve your problem using two HiddenField
.
- Set the two HiddenField using js/jquery(as you prefer)
- Force the form submit in the js function(
form1.submit()
) - In the formLoad check if the HiddenFields are empty. If no run your method and then clear the HiddenFields
You can use the __doPostBack function to post to server. just make sure you add a server control to the page so the function is inserted. here is a small example that you can customize for your needs:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Try1.WebForm1" EnableEventValidation="false"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#lblInvoke').hover(
function () {
__doPostBack('<%= LinkButton1.ClientID %>', 'value1,value2');
}
);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lblInvoke">Hover This!</label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Style="display:none;">LinkButton</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Try1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string passedArgument = Request.Params.Get("__EVENTARGUMENT");
string[] paramsArray = passedArgument.Split(',');
Label1.Text = string.Format("Returned from server. params: {0},{1}", paramsArray[0], paramsArray[1]);
}
}
}
Make sure you add the EnableEventValidation="false" attribute to page.
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"];
SelectParticipantEventArgs 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.
0 commentaires:
Enregistrer un commentaire