-2

i have created dynamic listboxes (4 to 10) in ASP.NET. and my question is , How do i find the dynamically created listboxes using c#?

thanks

Sure... and i appreciate your help . below code i am using for creating dynamic LB

protected void btndyfilter_Click(object sender, EventArgs e)
        {


            int numberOfListBox = lbFilter.GetSelectedIndices().Length;
            string lbname = lbFilter.SelectedValue;
            for (int i = 0; i < numberOfListBox; i++)
            {
                ListBox listb = new ListBox();
                ListItem lItem = new ListItem();
                listb.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;

                listb.Height = 150;
                listb.Width = 200;
                lItem.Value = i.ToString();

                lItem.Text = lbname;
                listb.Items.Add(lItem);
                panFilter.Controls.Add(listb);
                //once we created the LB dynamically i need to populate each LB with the corresponding values
                connstr2 = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
                conn2.ConnectionString = connstr2;
                conn2.Open();
                CubeCollection CubeList = conn2.Cubes;
                string cb = ddlCubeList.SelectedItem.Text;
                //need to remove the Hardcoded Code
                foreach (Member dimem in CubeList[cb].Dimensions["Date"].Hierarchies["Calendar Date"].Levels["Date"].GetMembers())
                {
                    ListItem Memlist = new ListItem();
                    Memlist.Text = dimem.UniqueName;
                    lbFilter.Items.Add(Memlist);

                }

            }
            panFilter.Visible = true;
            panCubeDef.Visible = true;
        }

so this will create the LB i believe :)... and Inside the commented code i need to use to populate for each LB item ..perhaps it bit hardcoded which i need to remove. so i all dynamic LBs are populated then the selected items from all LBs will come into the where clause in my MDX query..hope i did not confuse you

  • 1
    It might be helpful for you to add some more detail about what you're trying to do – Ortund Nov 26 '12 at 07:06
  • sure and thanks for looking into it.. basically what i am trying to do is, i have a filter section on my webpage and those filter values have to be selected from the listbox item that i dynamically created. so for that purpose i need to find the dynamic listbox and its items , later i can construct my where clause query. hope this helps you to help me :) – user1849129 Nov 26 '12 at 07:10
  • @user1849129 Can you please remove the comments ? and also tell us, where do you wont to use that dynamic created controls. – Aristos Nov 26 '12 at 10:46

3 Answers3

2

There is 2 way either you can store dynamic control detail with dictionary or just find when you want to use it using some code like this

Control GetControlByName(string Name)
{
foreach(Control c in this.Controls)
    if(c.Name == Name)
        return c;

return null;
}
  • This can work only if you have static controls, or after you have created. In dynamic creation of a control, there is no reason to search for it again - you know it because you create it at that moment. – Aristos Nov 26 '12 at 07:23
0

while generating ListBox dynamically, give ListBox ID as: lstBoxNo1, lstBoxNo2. lstBoxNo3 etc. where 1,2,3(no) will be from count. like

int count=1;

generate listbox control

listboxid=lastBoxNo+count;

count++

`by doing this, u have control over id's.

else use http://stackoverflow.com/questions/3731007/using-findcontrol-to-find-control using this link to understand findcontrol.

vikbehal
  • 1,486
  • 3
  • 21
  • 48
0

The points that you wont to find that dynamic controls are.

  1. The moment you first render the page.
  2. On every other post back.

In the case of 1, then you better keep a variable on your page that keep that creations.

In the case of 2, when you have post back, you need to store somehow the creations of your control in the page when you render it. One good place is to keep that information on the viewstate.

You can also on the post back, just to check if you have any post back valued from controls that you have named with a serial numbering starting from 1, eg You start looking if you have post back from ControlName_1, then ControlName_2, and when you not found any other value you end.

Aristos
  • 66,005
  • 16
  • 114
  • 150