manjeet799
06-08-2006, 12:40 PM
Hi,
I am creating some controls on run time on click event of button.
I create two drop downs and one button on the click of existing button place on the form at design time.
The problem is :
1) I am not able to specify the click event of dynamicly generated button.
Alternatively I tried generating controls on the clickevent of same button again and again.
Then new problem is that
When i click the button first time it generates the controls,
but when clicked second time page is posted back and I call same function with different parameter, controls previously generated disappear and new controls come.
Please tell me how can i genrate these controls.
I don't want to loose previously generated controls.
The code is here
protected void btnAddRows_Click(object sender, EventArgs e)
{
int i = Convert.ToInt16 (Session["i"]);
Response.Write("AAA");
fnGenerateRows(i);
}
// function that genrated the rows and controls
public void fnGenerateRows(int i)
{
// Total number of rows
Response.Write("BBB");
int rowCtr;
// Total number of cells per row (columns)
int cellCtr;
//int ag = 0;
for (rowCtr = i; rowCtr < i+1; rowCtr++)
{
HtmlTableRow tRow = new HtmlTableRow();
TABLE1.Rows.Add(tRow);
int k = i;
for (cellCtr = k; cellCtr < k+2; cellCtr++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Label lbl = new Label();
lbl.EnableViewState = true;
if (cellCtr == k && rowCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "lblDestination" + rowCtr + cellCtr;
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (cellCtr == k+1 && rowCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "lblDestination" + rowCtr + cellCtr;
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (rowCtr == k+1 && cellCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "Destination6";
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (rowCtr == k+1 && cellCtr == k+1)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "Destination7";
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
}
HtmlTableCell tCell1 = new HtmlTableCell();
tRow.Cells.Add(tCell1);
Button btn = new Button();
btn.ID = "btn" + rowCtr + cellCtr;
btn.Text = Convert .ToString (++k);
k--;
//btn.Click += new System.EventHandler(ButtonClick);
tCell1.Controls.Add(btn);
}
i++;
Session["i"] = i;
}
I am getting the value of i from session and updating it to give controls different id
I am creating some controls on run time on click event of button.
I create two drop downs and one button on the click of existing button place on the form at design time.
The problem is :
1) I am not able to specify the click event of dynamicly generated button.
Alternatively I tried generating controls on the clickevent of same button again and again.
Then new problem is that
When i click the button first time it generates the controls,
but when clicked second time page is posted back and I call same function with different parameter, controls previously generated disappear and new controls come.
Please tell me how can i genrate these controls.
I don't want to loose previously generated controls.
The code is here
protected void btnAddRows_Click(object sender, EventArgs e)
{
int i = Convert.ToInt16 (Session["i"]);
Response.Write("AAA");
fnGenerateRows(i);
}
// function that genrated the rows and controls
public void fnGenerateRows(int i)
{
// Total number of rows
Response.Write("BBB");
int rowCtr;
// Total number of cells per row (columns)
int cellCtr;
//int ag = 0;
for (rowCtr = i; rowCtr < i+1; rowCtr++)
{
HtmlTableRow tRow = new HtmlTableRow();
TABLE1.Rows.Add(tRow);
int k = i;
for (cellCtr = k; cellCtr < k+2; cellCtr++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Label lbl = new Label();
lbl.EnableViewState = true;
if (cellCtr == k && rowCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "lblDestination" + rowCtr + cellCtr;
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (cellCtr == k+1 && rowCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "lblDestination" + rowCtr + cellCtr;
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (rowCtr == k+1 && cellCtr == k)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "Destination6";
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
else if (rowCtr == k+1 && cellCtr == k+1)
{
lbl.ID = "lblDestination" + rowCtr + cellCtr;
lbl.Text = "Destination7";
tCell.Controls.Add(lbl);
DropDownList ddlDestination = new DropDownList();
ddlDestination.ID = "ddlDestination" + rowCtr + cellCtr;
tCell.Controls.Add(ddlDestination);
}
}
HtmlTableCell tCell1 = new HtmlTableCell();
tRow.Cells.Add(tCell1);
Button btn = new Button();
btn.ID = "btn" + rowCtr + cellCtr;
btn.Text = Convert .ToString (++k);
k--;
//btn.Click += new System.EventHandler(ButtonClick);
tCell1.Controls.Add(btn);
}
i++;
Session["i"] = i;
}
I am getting the value of i from session and updating it to give controls different id