the important thing is to recreate those controls upon postback, so it is important to do it in Page_Load or before, for example,
void Page_Load(Object sender, EventArgs e)
{
  //get data from the database  int nCount = ..; //might need to save nCount inside a ViewState or Session variable  for (int i=0; i < nCount; i++)
  {
    ListBox lb = new ListBox();
    lb.ID = "lb" + i.ToString();
    lb.SelectedIndexChanged += new EventHandler(YourHandler);
    form1.Controls.Add(lb);
    if (!IsPostBack) //or the first time you create them
    {
         lb.DataSource = ...;
         ...
         lb.DataBind();
    }
  }}

解决方案 »

  1.   

    void YourHandler (object sender, EventArgs e)
    {
      ListBox lb = (ListBox)sender;
      Response.Write(lb.ID);
    }
      

  2.   

    also doListBox lb = new ListBox();
    lb.AutoPostBack = true;
    lb.ID = "lb" + i.ToString();
    ....
      

  3.   

    ListItem item;
    for(int i=0;i<ds.Tables["template"].Rows.Count;i++)
    {
    item=new ListItem(ds.Tables["template"].Rows[i]["var_TemplateName"].ToString(),ds.Tables["template"].Rows[i]["int_TemplateID"].ToString());
    Lis_Template.Items.Add(item);
    }