net/C# application I have a Link button:
<asp:LinkButton runat="server" ID="LinkButton1"/>
When the user clicks this button I want to execute this javascript function:
document.getElementById('LinkButtonPrevious').click();
And change a Session variable:
Session["ID"] = 2;
The problem is that the session variable can only be changed in the code behind.
How can I execute both when the link button is clicked?
Thanks in advance
Changing a value server side requires either a full post of the page or an ajax call.
So you could add a HiddenField and set its value to 2 client side, and then in your server side button handler use that value to set the session variable.
Or you could do an ajax call to a web service.
As far a performing client and server side operations on the same click event, you can do the following. Add a server side Click handler as usual, then register a client side event with ClientScriptManager.RegisterOnSubmitStatement. Here’s the example from MSDN: