1

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

2 Answers 2

4

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:

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client script on the page.
    String csname = "OnSubmitScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the OnSubmit statement is already registered.
    if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
    {
      String cstext = "document.write('Text from OnSubmit statement');";
      cs.RegisterOnSubmitStatement(cstype, csname, cstext);
    }

  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     <input type="submit"
            value="Submit" />
     </form>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

I know that. I was asking if both the above functions can be done at the same time when I click a button
I updated my answer with information on ClientScriptManager.RegisterOnSubmitStatement().
1

You May have to Use ClientCallBack ...

here is an example of it..

MSDN lINK FOR CLIENT CALLBACK

<script type="text/ecmascript">
    function LookUpStock() {
        var lb = document.getElementById("ListBox1"); // this two statements set your javascript
        var product = lb.options[lb.selectedIndex].text;
        CallServer(product, ""); //this method contains code where you set session variable
    }

    function ReceiveServerData(rValue) {
        document.getElementById("ResultsSpan").innerHTML = rValue;
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.