Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列出ASP.NET中的字符串会话变量_C#_Asp.net_Variables_Session - Fatal编程技术网

C# 列出ASP.NET中的字符串会话变量

C# 列出ASP.NET中的字符串会话变量,c#,asp.net,variables,session,C#,Asp.net,Variables,Session,我正在尝试在ASP.net中制作一个程序,其中用户在第1页的文本框(default.aspx)中输入详细信息,单击一个按钮,它就会出现在第2页的列表框(about.aspx)中 我正在尝试这样做,用户可以在第1页的文本框中输入尽可能多的内容,它们都会出现在第2页的列表框中 第1页上的代码: public void Button1_Click(object sender, EventArgs e) { people = Session["mySession"] as List<str

我正在尝试在ASP.net中制作一个程序,其中用户在第1页的文本框(default.aspx)中输入详细信息,单击一个按钮,它就会出现在第2页的列表框(about.aspx)中

我正在尝试这样做,用户可以在第1页的文本框中输入尽可能多的内容,它们都会出现在第2页的列表框中

第1页上的代码:

public void Button1_Click(object sender, EventArgs e)
{
    people = Session["mySession"] as List<string>;
    people.Add(TextBox1.Text);
}
public void按钮1\u单击(对象发送者,事件参数e)
{
people=会话[“mySession”]作为列表;
添加(TextBox1.Text);
}
第2页上的代码:

protected void Page_Load(object sender, EventArgs e)
{
    var myList = Session["mySession"] as List<string>;
            ListBox1.Text = string.Join(",", myList);
}
受保护的无效页面加载(对象发送方,事件参数e)
{
var myList=Session[“mySession”]作为列表;
ListBox1.Text=string.Join(“,”,myList);
}
任何帮助都很好。

问题在于:

ListBox1.Text = Session["mySession"] as List<string>;
ListBox1.Text=Session[“mySession”]作为列表;
您无法将字符串列表转换为字符串-另一方面,您可以决定显示所有字符串,例如

var myList = Session["mySession"] as List<string>;
ListBox1.Text = string.Join(",", myList);
var myList=Session[“mySession”]作为列表;
ListBox1.Text=string.Join(“,”,myList);
问题在于:

ListBox1.Text = Session["mySession"] as List<string>;
ListBox1.Text=Session[“mySession”]作为列表;
您无法将字符串列表转换为字符串-另一方面,您可以决定显示所有字符串,例如

var myList = Session["mySession"] as List<string>;
ListBox1.Text = string.Join(",", myList);
var myList=Session[“mySession”]作为列表;
ListBox1.Text=string.Join(“,”,myList);

您必须检查会话中是否有任何内容。。。点击按钮:

people = Session["mySession"] as List<string>;
//Create new, if null
if(people == null) 
    people = new List<string>();

people.Add(TextBox1.Text);

Session["mySession"] = people;
people=Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
添加(TextBox1.Text);
会话[“我的会话”]=人;
这是第一次。第二,在第2页,您必须在页面加载时执行以下操作:

people = Session["mySession"] as List<string>;
//Create new, if null
if(people == null) 
people = new List<string>();
ListBox1.DataSource = people;
ListBox1.DataBind();
people=Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
ListBox1.DataSource=人;
ListBox1.DataBind();
当然,如果您在这一部分中使用某种静态方法来解决代码重复问题,效果会更好:

public static List<string> GetPeopleFromSession(){
    var people = HttpContext.Current.Session["mySession"] as List<string>;
    //Create new, if null
    if(people == null) 
        people = new List<string>();
    return people;
}
公共静态列表GetPeopleFromSession(){
var people=HttpContext.Current.Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
还人,;
}

您必须检查会话中是否有任何内容。。。点击按钮:

people = Session["mySession"] as List<string>;
//Create new, if null
if(people == null) 
    people = new List<string>();

people.Add(TextBox1.Text);

Session["mySession"] = people;
people=Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
添加(TextBox1.Text);
会话[“我的会话”]=人;
这是第一次。第二,在第2页,您必须在页面加载时执行以下操作:

people = Session["mySession"] as List<string>;
//Create new, if null
if(people == null) 
people = new List<string>();
ListBox1.DataSource = people;
ListBox1.DataBind();
people=Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
ListBox1.DataSource=人;
ListBox1.DataBind();
当然,如果您在这一部分中使用某种静态方法来解决代码重复问题,效果会更好:

public static List<string> GetPeopleFromSession(){
    var people = HttpContext.Current.Session["mySession"] as List<string>;
    //Create new, if null
    if(people == null) 
        people = new List<string>();
    return people;
}
公共静态列表GetPeopleFromSession(){
var people=HttpContext.Current.Session[“mySession”]作为列表;
//如果为空,则创建新的
if(people==null)
人员=新列表();
还人,;
}

感谢您的反馈,在我的代码中更改了这一点,但在人员上仍有错误。添加(TextBox1.Text)@user2975108-第一次使用会话时,您必须在其中放置一些内容。请检查我下面的答案,它检查会话中是否有数据,如果没有,则创建新的字符串列表。直接复制粘贴必须工作…感谢您的反馈,在我的代码中更改了这一点,但在人员上仍然有一个错误。添加(TextBox1.Text)@user2975108-第一次使用会话时,您必须在其中放置一些内容。请检查我下面的答案,它检查会话中是否有数据,如果没有,则创建新的字符串列表。直接复制粘贴必须工作。。。