Question : Add dropdownlists to a page automatically and preserve them between postbacks

Hi,

I have a web development project that requires drilling down into categories which may be several layers deep, so my idea was to start with one dropdownlist which was hard wired on the page, but any subsequent sub categories (based on parent category selection) would be displayed in dropdownlists that would be dynamically added to the page.

To save me a lot of time, if anyone has done this in(vb.net or c#) I would be extremely grateful for some code. Or if you dont have anything already carved out a pointer to a very good resource would be very helpful.

Thanks..

Answer : Add dropdownlists to a page automatically and preserve them between postbacks

here is the code
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
**** c# ***

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            CreateCtrls();

    }
    private void CreateCtrls()
    {
        DropDownList ddl1 = new DropDownList();
        ddl1.ID = "ddl1";
        ddl1.Items.Add("asdf0");
        ddl1.Items.Add("asdf1");
        ddl1.Items.Add("asdf2");
        ddl1.Items.Add("asdf3");
        pnl.Controls.Add(ddl1);
        DropDownList ddl2 = new DropDownList();
        ddl2.ID = "ddl2";
        ddl2.Items.Add("lkjh0");
        ddl2.Items.Add("lkjh1");
        ddl2.Items.Add("lkjh2");
        ddl2.Items.Add("lkjh3");
        pnl.Controls.Add(ddl2);

        ViewState["ControlsAdded2"] = true;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {

    }
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["controlsadded1"] == null)
        {
            CreateCtrls();
        }
    }

**** aspx ***
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel runat="server" ID="pnl">
        </asp:Panel>
        <asp:Button runat="server" ID="btn1" onclick="btn1_Click" Text="click me" />
    </div>
    </form>
</body>
Random Solutions  
 
programming4us programming4us