tags:

views:

330

answers:

2

Hi, I want to change an ASP.NET variable when someone clicks on a link on the page. Specifically a Session["something"] variable. That means I should do it after postback, but I am new to whole web development and the ASP.NET MVC specifically, so I know just an overloaded action with some [Accept] attribute. Since this link is on the master site, I can imagine only to add such an action to each action existing and that seems pretty unfortunate. Javascript doesn't have access to ASP.NET variables, so I am pretty lost.

This action link should be for almost every action and I don't know, how to recognize, whether it has been accesses by this spoecific or some other link (which is important to me). That implies some postprocessing, something like


<a href="" onclick="<% Session["location"] = new_value %>" >

Any ideas? Piece of code?

Thanks in advance.

A: 

You could possibly use a LinkButton and have it fire a server function

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click" />

Then your serverside function will look something like this:

protected void lkbChange_Click(Object sender, EventArgs e)
{
    Session["location"] = new_value;
}
d1k_is
Can I send parameters to the server function? And where in the code should the function existest? In behind code of the View?
Trimack
This answer is for ASP.NET. The question is about ASP.NET MVC where the concept of events is not present.
Malcolm Frexner
sorry, i missed that...
d1k_is
+1  A: 
  1. An actionlink is just a helper method to create an <a href="... tag. You pass it the controller and action you want, and it uses your routing table to determine how to build the href attribute.
  2. To do what you want, your controller will need to change the session variable.

So, if the fact that a controller has been called is enough to figure out what the new session variable should be, then that's easy -- simply change the session variable in the action.

However, it might be that you want different links to the same action to set different session variables. In that case, your actinlink might look something like this:

<%= Html.ActionLink("Click Me", "action_name", "controller", new {session="new_value"}) %>

which causes something like this (depending on your route table):

<a href="/controller/action_name?session=new_value">Click Me</a>

So now you have to set your session variable in your action:

public ActionResult action_name(string session)
    {
        Session["val"] = session;
        ....

EDIT:

Considering your comment, there are two ways to go about it: an attribute decorating controllers or a master controller. I'd recommend the master controller as it'd probably be easiest for you to implement.

Since you didn't include any specific examples, I'll make one up. It's where, regardless of which action is called, a particular querystring equates to a particular value in the session, like for setting language.

I'm going from memory here, so you might have to experiment a bit (especially with where exactly the session and request objects are). If not, I can look at my code later. If you don't already have one, create a master controller that your other controllers derive from. Override initialize (calling base.Initialize within it). And then take a look at the querystrings, like:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);

        if (requestContext.Current.Request["language"])
           requestContext.Current.Session["language"] = HttpContext.Current.Request["language"];
    }

James

James S
The second case is right, but as I tried to explain in my question, the session variable should be changed in almost each action in the controller and it doesn't seem clever to me to use a parameter session in each action probable to be needed.
Trimack
I really like this, but one more thing - how would the actionlink tag look like in order to send the value? Still the same?
Trimack
Yes. though writing "new {language = "english"}" might get tiring, so you create an HTML helper (public string Language(this HtmlHelper helper, string language) { return new {language = language}; }. then in your ActionLink you can have Html.Language("english") in place of the anonymous object.
James S
Sorry to bother, but still not enough.. I can't find any example of master controller. Where should I place it? How to make it the "master"??
Trimack
A master controller is simply a class that fits between the controller class (defined in the MVC code) and your class. So, instead of your controller deriving from Controller, it derives from MasterController, which derives from Controller. (public class MasterController : Controller { })
James S