如何在会话变量中保存多个值

发布于 2024-12-24 18:54:02 字数 601 浏览 2 评论 0原文

在我的项目中,我想要存储最近访问的 ID(例如 CompanyId),并根据 Id,我需要在 aspx 网页上显示最近的 5 条记录。为此,我在会话类中使用如下会话变量:

public static string RecentAssetList
{
    get
    {
        return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();
    }
    set
    {
        HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;
    }
}

并且从我的页面存储值到会话中,如下所示。

    string assetList = "";            
    assetList += assetId.ToString() + ",";
    SessionData.RecentAssetList = assetList;

但它每次只存储一个 ID,如果有新记录访问会话,则仅显示新的 ID。 如何根据我想要从数据库获取数据并显示在网格中的值在会话中存储多个值。

In my project i want store recently accessed Id's(like CompanyId) and based on Id's i need to display most recent 5 records on aspx webpage.For this i am using a session variable like below in my session class:

public static string RecentAssetList
{
    get
    {
        return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();
    }
    set
    {
        HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;
    }
}

And from my page storing the values into session like below.

    string assetList = "";            
    assetList += assetId.ToString() + ",";
    SessionData.RecentAssetList = assetList;

but it stores only one Id every time,if new record accessed session showing new one only.
How can i store multiple values in my session based that values i want to get data from Database and display in my grid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贩梦商人 2024-12-31 18:54:02

您应该首先读取之前存储的值:

string assetList = SessionData.RecentAssetList;            
assetList += assetId.ToString() + ",";
SessionData.RecentAssetList = assetList;

但是这个解决方案并不完美,因为新的 ID 将永远附加。首先更好地解析和解释:

string assetList = SessionData.RecentAssetList;            
var ids = assetList.Split(',').ToList().Take(10).ToList();
ids.Add(assetId);
ids = ids.Distinct().ToList();
assetList = string.Join(",", ids);
SessionData.RecentAssetList = assetList;

You should read the previously stored value first:

string assetList = SessionData.RecentAssetList;            
assetList += assetId.ToString() + ",";
SessionData.RecentAssetList = assetList;

But this solution isn't perfect because new IDs will be appended forever. Better parse and interpret first:

string assetList = SessionData.RecentAssetList;            
var ids = assetList.Split(',').ToList().Take(10).ToList();
ids.Add(assetId);
ids = ids.Distinct().ToList();
assetList = string.Join(",", ids);
SessionData.RecentAssetList = assetList;
落花浅忆 2024-12-31 18:54:02

我不确定我完全理解,但是您可以在会话中存储字典:

public static Dictionary<int, int[]> RecentAssetLists
{
    get
    {
        var session = HttpContext.Current.Session;

        var dict = session["RECENT_ASSET_LISTS"];

        if (dict == null)
        {
            dict = new Dictionary<int, int[]>();
            session["RECENT_ASSET_LISTS"] = dict; 
        }

        return dict;
    }
}

或者您可以定义自己的自定义对象并将其存储在会话中:

[Serializable]
public sealed class RecentAssetList
{
    private List<int> assets = new List<int>();

    public List<int> RecentAssets 
    {
        get { return this.assets; }
    }
}

public static RecentAssetList RecentAssetList
{
    get
    {
        var session = HttpContext.Current.Session;

        var assetlist = session["RECENT_ASSET_LIST"];

        return (RecentAssetList)assetlist;
    }
}

I'm not sure I fully understand, but you can store a dictionary in the session:

public static Dictionary<int, int[]> RecentAssetLists
{
    get
    {
        var session = HttpContext.Current.Session;

        var dict = session["RECENT_ASSET_LISTS"];

        if (dict == null)
        {
            dict = new Dictionary<int, int[]>();
            session["RECENT_ASSET_LISTS"] = dict; 
        }

        return dict;
    }
}

Or you can define your own custom object and store that in the session:

[Serializable]
public sealed class RecentAssetList
{
    private List<int> assets = new List<int>();

    public List<int> RecentAssets 
    {
        get { return this.assets; }
    }
}

public static RecentAssetList RecentAssetList
{
    get
    {
        var session = HttpContext.Current.Session;

        var assetlist = session["RECENT_ASSET_LIST"];

        return (RecentAssetList)assetlist;
    }
}
小霸王臭丫头 2024-12-31 18:54:02

最简单的答案是(您仍然需要应用逻辑来仅获取最后 5 个,请参阅我的第二个示例,其中它已经应用了逻辑:

string assetList = SessionData.RecentAssetList;             
assetList += assetId.ToString() + ",";     
SessionData.RecentAssetList = assetList; 

因此在添加值之前,您检索已添加到会话中的值。

您还可以更改RecentAssetList成为 List ,这样您就不必手动追加它,而只需添加到列表中,

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

        return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();   
    }   
    set   
    {   
        HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;   
    }   
}   

然后添加:

List<string> assetList = SessionData.RecentAssetList;             
assetList.Insert(0, assetId.ToString());     
SessionData.RecentAssetList = assetList.Take(5); // otherwise it's not changed in the session, you can write an extension method where you can add it to the list and submit that list to the session at the same time

.Take(5);。 > 是这样你只需要直到最后插入的 5 个值

easiest answer would be (you will still have to apply the logic to only take the last 5, see my second example where it already applies the logic:

string assetList = SessionData.RecentAssetList;             
assetList += assetId.ToString() + ",";     
SessionData.RecentAssetList = assetList; 

so before adding a value you retrieve the values already added to session.

You can also change RecentAssetList to be a List<string> so that you don't have to manually append it, instead just adding to the list.

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

        return HttpContext.Current.Session["RECENT_ASSET_LIST"].ToString();   
    }   
    set   
    {   
        HttpContext.Current.Session["RECENT_ASSET_LIST"] = value;   
    }   
}   

And then to add:

List<string> assetList = SessionData.RecentAssetList;             
assetList.Insert(0, assetId.ToString());     
SessionData.RecentAssetList = assetList.Take(5); // otherwise it's not changed in the session, you can write an extension method where you can add it to the list and submit that list to the session at the same time

.Take(5); is so that you only take up until the last 5 values inserted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文