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

本文关键字:动态 创建 列表 Net ASP | 更新日期: 2023-09-27 17:59:36

    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);
    }

第一个列表框selectedindexchanged事件工作,但第二个列表框selectedindexchanged事件不工作。如果我在第二个列表框上选择了一个选项,第二个对话框就会丢失。只需将选中的选项保留在第一个列表框中即可。我该怎么办?请帮帮我。

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

我希望能帮助你

    Panel pnl = new Panel();
    ListBox lst1 = new ListBox();
    ListBox lst2 = new ListBox();
    ListBox lst3 = new ListBox();
    protected override void OnInit(EventArgs e)
    {
        lst1.Attributes["class"] = "cat-list";
        lst1.AutoPostBack = true;
        lst1.SelectedIndexChanged += Lst_SelectedIndexChanged;
        lst2.Attributes["class"] = "cat-list";
        lst2.AutoPostBack = true;
        lst2.SelectedIndexChanged += Lst_SelectedIndexChanged;
        lst2.Visible = false;
        lst3.Attributes["class"] = "cat-list";
        lst3.AutoPostBack = true;
        lst3.SelectedIndexChanged += Lst3_SelectedIndexChanged;
        lst3.Visible = false;
        pnl.Attributes["class"] = "col-sm-2 col-xs-12";
        pnl.Controls.Add(lst1);
        pnl.Controls.Add(lst2);
        pnl.Controls.Add(lst3);
        categories.Controls.Add(pnl);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lst1.DataSource = cat.list(Convert.ToInt32(0));
            lst1.DataTextField = "catName";
            lst1.DataValueField = "catId";
            lst1.DataBind();
        }
    }
    private void Lst_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox listBox = (ListBox)sender;
        ListBox lst;
        if (listBox == lst1)
            lst = lst2;
        else
            lst = lst3;
        lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
        lst.DataTextField = "catName";
        lst.DataValueField = "catId";
        lst.DataBind();
        lst.Visible = true;
    }
    private void Lst3_SelectedIndexChanged(object sender, EventArgs e)
    {
        //your code for third Listbox
    }