如何从会话变量中获取数据? [英] How to get data from session variable?

查看:61
本文介绍了如何从会话变量中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个函数,当我检查一个复选框控件时,我创建了cart会话,哪些故事列表的数据和那些数据,我想得到product_id。



当我尝试执行以下函数时,我在下面的行中收到一个强制转换异常错误:

System.InvalidCastException:无法将'System.Collections.Generic.List`1 [System.String]'类型的对象强制转换为'System.String'。

< pre lang =text> string data =(string)Session [Cart];





我也尝试从复选框变量中调用数据,但我也不断收到另一个错误,通知我输入格式不正确。

整个方法:

 受保护 < span class =code-keyword> void  AddToCart_Click( object  sender,EventArgs e)
{
var selectedProducts = GridView1.Rows.Cast< GridViewRow>()
.Where(row = > ((CheckBox )row.FindControl( SelectedProducts))。已检查)
.Select(row = > GridView1.DataKeys [row.RowIndex] .Value.ToString())。ToList();
if (会话[ 购物车] == null
{
会话[ 购物车] = selectedProducts;
}
else
{
List< String> cart = new List< String>();
cart =(List< string>)会话[ 购物车];
// cart.Add(会话[购物车]);
foreach var 产品 selectedProducts)
cart.Add(product);
会话[ 购物车] =购物车;
}
foreach (GridViewRow行 GridView1.Rows)
{
CheckBox cb =(CheckBox)row.FindControl( SelectedProducts);
if (cb.Checked)
cb.Checked = true ;

{
string data =( string )会话[< span class =code-string> 购物车];
Label1.Text = data.ToString();

// 访问chb.Text;
// int num = Convert.ToInt32(cb.Text);
// Label1.Text = num.ToString();
}
}
}



任何帮助,将非常感谢。

非常感谢

解决方案

为什么你会期望它为你转换一个字符串列表?

如果你这样写:

 List< string> list =  new  List< string>(); 
string s =( string )list;

你会期待错误woudln;你呢?

所以当你这样做时:

  var  selectedProducts = GridView1.Rows.Cast< gridviewrow>()
.Where(row = > ((CheckBox)row.FindControl( SelectedProducts))。已选中)
.Select(row = > GridView1.DataKeys [row.RowIndex] .Value.ToString())。ToList();
if (会话[ 购物车] == null
{
会话[ 购物车] = selectedProducts;
...
string data =( string )会话[ 购物车]; < / gridviewrow >



这正是您的尝试要做什么!

尝试从会话中获取列表?

列表< string> data =(List< string>)会话[ 购物车]; < /  字符串 >  < /   string  >  


分配数据

会话[购物车] = selectedProducts;



检索数据

string data =(string)Session [Cart];


I am trying build a function, when I check a checkbox control, i create cart session which stories list of data and from that data, I would like to get product_id.

when I try to execute the below function, I get a cast exception error on the line below:
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String'.

string data = (string)Session["Cart"];



I tried calling data from the checkbox variable as well, but i also kept getting another error, informing me that the input format is incorrect.
Whole Method:

protected void AddToCart_Click(object sender, EventArgs e)
{
    var selectedProducts = GridView1.Rows.Cast<GridViewRow>()
      .Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
      .Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
    if (Session["Cart"] == null)
    {
        Session["Cart"] = selectedProducts;
    }
    else
    {
        List<String> cart = new List<String>();
        cart = (List<string>)Session["Cart"];
       // cart.Add(Session["Cart"]);
        foreach (var product in selectedProducts)
            cart.Add(product);
        Session["Cart"] = cart;
    }
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
        if (cb.Checked)
            cb.Checked = true;

            {
              string data = (string)Session["Cart"];
                Label1.Text = data.ToString();

                // Access chb.Text;
                //int num = Convert.ToInt32(cb.Text);
               // Label1.Text = num.ToString();
            }
    }
}


Any help, would be very much appreciated.
Many thanks

解决方案

Why would you expect it to convert a List of strigns to a string for you?
If you wrote this:

List<string> list = new List<string>();
string s = (string) list;

You would expect an error woudln;t you?
So when you do this:

            var selectedProducts = GridView1.Rows.Cast<gridviewrow>()
              .Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
              .Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
            if (Session["Cart"] == null)
            {
                Session["Cart"] = selectedProducts;
...
                      string data = (string)Session["Cart"];</gridviewrow>


That is exactly what your are trying to do!
Try getting the list from the session instead?

List<string> data = (List<string>)Session["Cart"];</string></string>


Assign Data
Session["Cart"] = selectedProducts;

Retrieve Data
string data = (string)Session["Cart"];


这篇关于如何从会话变量中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆