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
.form1.submit()
)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:
code behind:
Make sure you add the EnableEventValidation="false" attribute to page.
Here's how I did it...
And on the server side...
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.