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();
}
}}
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();
}
}}
{
ListBox lb = (ListBox)sender;
Response.Write(lb.ID);
}
lb.AutoPostBack = true;
lb.ID = "lb" + i.ToString();
....
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);
}