1

in my page1.aspx.cs I have this

  resultsHtml.Append("<a class='btn btn-warning' href='report.aspx?asset=" + rd["n_asset_id"].ToString() + "'><i class='fa fa-paw'></i></a> ");

so in my report.aspx I will catch the value.

Session["passThis"] = Request.QueryString["asset"];

However in my action.cs I created a class that will store the list. I'm thinking of adding a session there but everytime i will the store() it will just create another List.

public static void store() {

    List<string> ast = new List<string>();

}

How can I achieve this feat? I'm running out of ideas.

4
  • its because within your store() method you are creating a new list instead of adding data to an already created list, declare your list outside of the method and within the method set/get your session data. Commented Mar 20, 2017 at 3:55
  • I've think of that but how can I add the data to the list if it's outside, I believe it will throw an error because there is no variable such as the declared List? It's been 2 days and google can't answer Commented Mar 20, 2017 at 3:57
  • Put your declared list declaration before the method definition start. Commented Mar 20, 2017 at 4:01
  • In your store function first you can get the value from session (which is list considerably as you mentioned) and then you can add more values to list. After that you can reassign the updated list into session variable. Hope this will resolve your problem. If not, then please provide details with exact output that you want from scenario given by you. Commented Mar 20, 2017 at 6:39

1 Answer 1

1

I think that the best approach would be to extract assets list into property (so you can easily access it from other parts of code. In property getter, code checks if there's list in session.

In Store method you're accessing List of assets (List<string) and, if there is asset parameter in current Request, adds it to your list of assets. Notice that accessing Session is done through HttpContext.Current.Session because of static metod/property.

public static void Store()
{
    string assetValue = HttpContext.Current.Request["asset"];
    if (!string.IsNullOrEmpty(assetValue))
        AssestsList.Add(assetValue);


}

public static List<string> AssestsList
{
    get
    {
        if (HttpContext.Current.Session["assets"] == null)
        {
            HttpContext.Current.Session["assets"] = new List<string>();
        }

        return HttpContext.Current.Session["assets"] as List<string>;
    }
    set
    {
        HttpContext.Current.Session["assets"] = value;
    }
}

so, to use this, just call Store() from your code.

If you have some questions, feel free to ask.

Sign up to request clarification or add additional context in comments.

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.