Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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_Listbox_Selected - Fatal编程技术网

C# 如何在ASP.Net中动态创建列表框

C# 如何在ASP.Net中动态创建列表框,c#,asp.net,listbox,selected,C#,Asp.net,Listbox,Selected,第一个列表框selectedindexchanged事件工作,但第二个列表框selectedindexchanged事件不工作。若我在第二个列表框中选择了一个选项,第二个列表框将丢失。只需在第一个列表框中保留选中的选项即可。我能做些什么?请帮帮我。我希望这能帮助你 protected override void OnInit(EventArgs e) { ListBox lst = new ListBox(); lst.Attributes["c

第一个列表框
selectedindexchanged
事件工作,但第二个列表框
selectedindexchanged
事件不工作。若我在第二个列表框中选择了一个选项,第二个列表框将丢失。只需在第一个列表框中保留选中的选项即可。我能做些什么?请帮帮我。

我希望这能帮助你

    protected override void OnInit(EventArgs e)
    {
        ListBox lst = new ListBox();

        lst.Attributes["class"] = "cat-list";

        lst.DataSource = cat.list(Convert.ToInt32(0));
        lst.DataTextField = "catName";
        lst.DataValueField = "catId";
        lst.DataBind();


        lst.AutoPostBack = true;
        lst.SelectedIndexChanged += Lst_SelectedIndexChanged;

        Panel pnl = new Panel();
        pnl.Attributes["class"] = "col-sm-2 col-xs-12";
        pnl.Controls.Add(lst);

        categories.Controls.Add(pnl);
    }

    private void Lst_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox listBox = (ListBox)sender;
        ListBox lst = new ListBox();

        lst.Attributes["class"] = "cat-list";

        lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
        lst.DataTextField = "catName";
        lst.DataValueField = "catId";
        lst.DataBind();


        lst.AutoPostBack = true;
        lst.SelectedIndexChanged += Lst_SelectedIndexChanged;

        Panel pnl = new Panel();
        pnl.Attributes["class"] = "col-sm-2 col-xs-12";
        pnl.Controls.Add(lst);

        categories.Controls.Add(pnl);
    }
我希望这对你有帮助

    protected override void OnInit(EventArgs e)
    {
        ListBox lst = new ListBox();

        lst.Attributes["class"] = "cat-list";

        lst.DataSource = cat.list(Convert.ToInt32(0));
        lst.DataTextField = "catName";
        lst.DataValueField = "catId";
        lst.DataBind();


        lst.AutoPostBack = true;
        lst.SelectedIndexChanged += Lst_SelectedIndexChanged;

        Panel pnl = new Panel();
        pnl.Attributes["class"] = "col-sm-2 col-xs-12";
        pnl.Controls.Add(lst);

        categories.Controls.Add(pnl);
    }

    private void Lst_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox listBox = (ListBox)sender;
        ListBox lst = new ListBox();

        lst.Attributes["class"] = "cat-list";

        lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
        lst.DataTextField = "catName";
        lst.DataValueField = "catId";
        lst.DataBind();


        lst.AutoPostBack = true;
        lst.SelectedIndexChanged += Lst_SelectedIndexChanged;

        Panel pnl = new Panel();
        pnl.Attributes["class"] = "col-sm-2 col-xs-12";
        pnl.Controls.Add(lst);

        categories.Controls.Add(pnl);
    }

我们需要在哪里添加?在受保护的无效页面中加载(对象发送者,事件参数e),如果使用trired它,但这次第一个列表框选项丢失。这就是您需要的吗?首先感谢您的回复。我想在用户对已创建的旧listbox进行选择时创建listbox。此操作将继续进行,直到做出最终选择。我们需要在何处添加?在受保护的无效页面中加载(对象发送者,事件参数),如果使用trired,则加载,但这次第一个列表框选项丢失。这是您需要的吗?首先感谢您的回复。我想在用户对已创建的旧listbox进行选择时创建listbox。此操作将持续进行,直到做出最终选择。如果用户单击第二个列表框的某个项目,您希望看到什么?是否应该创建第三个列表框?如果用户单击第二个列表框的某个项目,您希望看到什么?它应该创建第三个列表框吗?