1

嗨,我正在尝试将我的会话放入下拉列表中,任何帮助都会很棒。目前它将数据放入标签中,我希望将其放入下拉列表中,每次单击按钮时都会添加一个新字符串,而不会删除最后一个

默认页面

protected void Button1_Click1(object sender, EventArgs e)
{
    Session["Fruitname"] = TbxName.Text; // my session i have made
}

输出页面

protected void Page_Load(object sender, EventArgs e)
{
    var  fruitname =  Session["Fruitname"] as String; // my session ive made
    fruit.Text = fruitname; // session used in lable
}

试过

           var myFruits = Session["Fruitname"] as List<string>;
        myFruits.Add(listbox1.Text);

但是当我尝试运行程序时出现错误

碎玻璃感谢您的帮助,它仍然没有做我需要的事情,但它到达了那里。

 var fruitname = Session["Fruitname"] as String; // my session ive made
           fruit.Text = string.Join(",", fruitname); // session used in lable

这就是工作。我需要一个下拉列表来显示放入 TbxName.Text 的所有字符串;输出成果实

4

2 回答 2

4

只需使用 aList<string>而不是字符串。

 var myFruits = Session["Fruitname"] as List<string>;
 myFruits.Add(TbxName.Text);
于 2013-11-09T22:38:19.113 回答
0

已使用在其他地方找到的代码修复

按钮页面代码如下

 protected void Button1_Click1(object sender, EventArgs e)
    {

       // Session["Fruitname"] = TbxName.Text; // my session i have made

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

        MyFruit.Add(TbxName.Text);

        Session["Fruitname"] = MyFruit;
{
public List<string> MyFruit { get; set; }
}

显示的页面

 protected void Page_Load(object sender, EventArgs e)
    {


        MyFruit = Session["Fruitname"] as List<string>;
        //Create new, if null
        if (MyFruit == null)
            MyFruit = new List<string>();
        ListBox1.DataSource = MyFruit;
        ListBox1.DataBind();


    }

    public List<string> MyFruit { get; set; }
}
于 2013-11-10T09:50:22.773 回答