Click to See Complete Forum and Search --> : problem with For loop in dynamically generating controls


meenakshi_joshi09
March 11th, 2007, 03:51 AM
Hi,
In my asp.net application I am dynamically generating some controls in the "For loop" such as:
Lable, TextBox and Button control.
The 'no of controls' depands upon the 'no of records' retrived from the database.

code:


for(int i=0;i<5;i++)
{
int ii=i+1;
Label lbl = new Label ();
lbl.ID = "label"+ii.ToString();
Panel1.Controls.Add(lbl);

TextBox txt=new TextBox();
txt.ID="txt"+ii.ToString();
Panel1.Controls.Add(txt);

Button btn=new Button();
btn.Click +=new EventHandler(btn_Click);
btn.ID="btn"+ii.Tostring();
btn.Text="DELETE";
Panel1.Controls.Add(btn);

}


In 'btn.Click' event I have written the code for deleting the records.

If there are 5 records in database then

5 Labels: label1,label2,label3,label4,label5.
5 TextBox: txt1,txt2,txt3,txt4,txt5
and
5 Button : btn1,btn2,btn3,btn4,btn5
will be created.
Now I want that when I click on any button such as btn3
the record of txt3 will be deleted. But when I am doing the same the record of last TextBox (txt5) is deleting.
I know that if "For loop" is executing 5 times then it getting the last value of label, textbox and button control, that is why it is deleting last record.

Can anybody help me in getting the value of that paticular button control which I have clicked and other controls corresponding to that button control?

'I also want to know how to use "Code Block" for writting my code with my thread which I am posting to the forum.'

thanks in advance

dglienna
March 11th, 2007, 04:03 AM
Quote this and see how it's done.

Highlight section, press Code button.

' this is code

that's it

meenakshi_joshi09
March 11th, 2007, 04:56 AM
Quote this and see how it's done.

Highlight section, press Code button.

' this is code

that's it


Thanks dglienna,
I have edited my post by using ....code block...... Now can you or anybody else help me in my first problem?

thanks again