Hello, this is my first post about such things and the reason for this is that i have to make some webparts in asp and i've tried everything but i can't make it work. What i need is a webpart that has a listbox with items generated after i get some info from another application and based on the selected index in the listbox a number of texboxes are generated. I then need to get the value from the texboxes after i click on a button. From what i've read on the interned I have to recreate the same tree of controls on every postback before viewstate loads so i called the control creation method in the OnInit event. The problem is that at that moment i can't retrieve the selected index of the listbox so i know which and how many textboxes to create. I tried to store the selected index in session but the selected index changed event of the listbox fires up after the OnInit method so I always get the selected index in the previous case. This is some of the code, please advise.
This is where i override OnInit so i can recreate the controls on every postback.
protected override void OnInit(EventArgs e)
{
if (this.Page.Session["index"] != null)
ListBox1.SelectedIndex = (int)this.Page.Session["index"];
makeCtrls(ListBox1.SelectedIndex + 1);
base.OnInit(e);
}
This is the method that creates the textboxes:
public void makeCtrls(int n)
{
tbList.Clear();
for (int i = 0; i < n; i++)
{
TextBox t = new TextBox();
t.ID = n.ToString() + i.ToString();
t.EnableViewState = true;
this.Controls.Add(t);
tbList.Add(t);
}
}
Method for the listbox:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
makeCtrls(ListBox1.SelectedIndex + 1);
this.Page.Session["index"] = ListBox1.SelectedIndex;
}