views:

163

answers:

6

I need to make a Function that receives a Session Key and a Session Value and call this function on a normal HTML onClick event. When this function is called the Session variable is assigned the Key I sent with the Value I sent. So far I have this:

<script runat="server" type="text/C#">
        protected void setSessionValue(string key, string value)
        {
            Session[key] = value;
        }  
</script>

But when I try to call if like so:

onclick="setSessionValue('itemID','3345');"

Its not working and giving me an error. Any help? I'm using c# and asp.net but I can't use code behind and need to work everything off the page.

+2  A: 

You cannot set anything to the session from the client because session in stored on the server.

Andrew Bezzub
Ok, is there any way at all to set call a server side function that does this? I need this to 'fire' when I press an onClick event and to send specific key/value information according to which onClick event is pressed. I don't mind if it means that the page needs to be refreshed.
William Calleja
Then you need to make a postback or ajax call to your page. Check out this article: http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx
Andrew Bezzub
+2  A: 

You are trying to call the server side (C#, VB.NET) code from the client side (Javascript).

This is not possible.

You can change your HTML control to be server side, by adding the runat="server" attribute on it - you will also need to change the type to one that is recognized serverside, namely <asp:HtmlLink />.

Oded
I added a 'runat="server"' attribute to my element (in this case an <a> tag) and it gave me a 'tag isn't well formed' error. any idea why? my <a> tag is within a databound object that needs to retrieve the session value from the database.
William Calleja
@William Calleja - Can you post the complete tag into your question as an update (you can edit your question to do so). BTW - my answer has been updated.
Oded
+2  A: 

You are trying to invoke a server method from a client side javascript. You can either do as what Oded suggested or, you can use the ASP.NET Ajax to achieve the same.

The below link would be a good starting point on how to do this using ASP.NET Ajax.

ASP.NET Ajax Exposing Webservices to Ajax

Update 1:

Another useful link

Ramesh
A: 

Try using

OnServerClick="setSessionValue('itemID','3345');"

instead of

onclick="setSessionValue('itemID','3345');"

Asad Butt
A: 

Here is a write-up on the difference between Client Side and Server Side ->

Difference between "Client Side" and "Server Side"

Morten Anderson
A: 

Ok I managed to find a solution on my own thanks to all of your submissions, listen to this. I added a javascript function as follows:

function setSessionValue(key, value) {
     $.post('setSession.aspx?key=' + key + '&value=' + value);
}

and added an aspx file with the following code within it:

<%@ Page Language="C#" %>
<% 
    Session[Request["key"]] = Request["value"]; 
%>

Basically this worked just fine, all I needed was a little jQuery. Thanks to all for your suggestions.

William Calleja