tags:

views:

47

answers:

3

Hi, I am curious, how do I pass a parameter to a server side function, which looks like this?

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

Then the serverside function will look something like this:

<script runat="server">
protected void lkbChange_Click(Object sender, EventArgs e)
{
    Session["location"] = new_value;
}
<script >

And I would like something like:

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click(<%= value %>)" />

protected void lkbChange_Click(Object sender, EventArgs e, string value)
{
    Session["location"] = value;
}

So there is also the problem to pass the parameter to the function call...

EDIT

Ok, i am beginning to feel that runat="server" is wrong. Well, I am presenting some machines and there properties. What I want here, is to filter the current resluts (of total machine's contents, history etc.) based on locations of these machines. That means the current action should stay the same - compute the same set of results, only filtered down to the currently selected location(s) - taken from the link, that changes the value of Session["location"] to e.g. everywhere.

+1  A: 

What is value? Is it some property of the LinkButton? If yes then you can access it through sender.

If it is something else, then AFAIK either use the way you have mentioned or you can write a custom event handler.

danish
Value is not a property of the linkbutton, rather just a value, that the linkbutton should save in the session variable, but thanks for a good idea of the property. I have made a mistake and it didn't work. probably know why now.
Trimack
+1  A: 

You can use CommandArgument and CommandName to achieve that.

<asp:LinkButton ID="lkbChange" runat="server" CommandArgument="MyArg"   
CommandName="ThisLnkClick" OnClick="MyHandler" />

You create the handler:

protected void MyHandler(Object sender, EventArgs e)
{

      Button lnkButton = (Button)sender;  
      //You could handle different command names by doing something like: if (lnkButton.CommandName == "ThisLnkClick") ...
      CustomMethod(lnkButton.CommandArgument.ToString());  
}

You then have to create your custom method that takes a parameter.

Max
+1  A: 

Ok, I'm still a little hazy on what you actually require but I'll give it a go (although I fear it will raise more questions than answers ;-).

In your global.asax.cs add a route similar to this:

routes.MapRoute(
    "MachineList",
    "Machines/AddLocationFilter/{filterValue}",
    new { controller = "Machines", action = "AddLocationFilter", filterValue = "" }
);

Then all your links in your view would be like (where 'value' is your filter value):

<%= Html.ActionLink("link text",
                    "AddLocationFilter",
                    new { filterValue = value })%>

Now in your Machines controller:

public ActionResult Index()
{
    //Get your machines here
    //NOTE: I have no idea how you want this to work and this is just an example of how it could work
    IQueryable<Machine> machines = myMachineRepository.GetAllMachines();

    //Use the "FilterLocations" session value if it exists
    if (Session["FilterLocations"] != null)
    {
        IList<string> filterLocations = Session["FilterLocations"] as List<string>
        machines.Where(m => Locations.Contains(m.Location))
    }

    //Now send the filtered list of machines to your view
    return View(machines.ToList());
}

public ActionResult AddLocationFilter(string filterValue)
{
    //If there isn't a filterValue don't do anything
    if (string.IsNullOrEmpty(filterValue))
        return RedirectToAction("index");

    IList<string> filterLocations;
    //Get it from session
    if (Session["FilterLocations"] != null)
        filterLocations = Session["FilterLocations"] as List<string>

    //If it's still null create a new one
    if (filterLocations == null)
        filterLocations = new List<string>();

    //If it doesn't already contain the value then add it
    if (!filterLocations.Contains(filterValue))
        filterLocations.Add(filterValue);

    //Finally save it to the session
    Session["FilterLocations"] = filterLocations;

    //Now redirect
    return RedirectToAction("index");
}

Savvy?

I'm not entirely happy with the redirect at the end but I did that for simplicity to try and help you understand things easier. You could do some much streamlined and sexy things with jQuery and returning a JSON result... but that's another level.

HTHs
Charles

Charlino
I really appriciate it, but this is not exactly it. There is a number of actions, on which this should work (redirect). If it is possible to redirect to the last used(for current user) action instead of fixes index, than it would be perfect.
Trimack
You mean something like: Redirect(Request.UrlReferrer.ToString()); ?
Charlino
Thats it :) I am new to .NET, so I get lost all the time :) Thanks again.
Trimack