このコードは最初の 2 回は正常に動作します。つまり、[ソリューションを追加] をクリックすると、それぞれ新しい行とコントロールが作成されますが、3 回目に [ソリューションを追加] をクリックし、それぞれの [ステップを追加] ボタンをクリックすると、それは、、、それは問題を作成します。
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < TotalNumberAdded; i++)
{
AddControls((i + 1), plcSolution);
}
for (int i = 0; i < TotalNumberSolAdded; i++)
{
AddMoreControls((i + 1), plcAddMoreSolution, 1);
Button b = (Button)plcAddMoreSolution.FindControl("btn" + (i+1));
for (int j = 0; j < TotalNumberSolStepAdded; j++)
{
AddMoreStepControls((j + 1), plcAddMoreSolution);
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
AddControls(TotalNumberAdded, plcSolution);
}
private void AddControls(int controlNumber, PlaceHolder plc)
{
TextBox txtBoxSolution = new TextBox();
Label lblSolution = new Label();
txtBoxSolution.ID = "txtBoxSolution" + controlNumber;
txtBoxSolution.TextMode = TextBoxMode.MultiLine;
txtBoxSolution.Width = 470;
txtBoxSolution.Height = 50;
lblSolution.ID = "lblSolution" + controlNumber;
lblSolution.Text = "Step " + (controlNumber + 1) + ": ";
lblSolution.Width = 200;
plc.Controls.Add(lblSolution);
plc.Controls.Add(txtBoxSolution);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
protected void btnAlternate_Click(object sender, EventArgs e)
{
lblSol.Text = "Solution 1";
string str = (string)ViewState["btnId"];
if (str != null)
{
btn = (Button)plcAddMoreSolution.FindControl(str);
btn.Visible = false;
}
btnAdd.Visible = false;
TotalNumberSolAdded++;
AddMoreControls(TotalNumberSolAdded, plcAddMoreSolution,1);
}
protected int TotalNumberSolAdded
{
get { return (int)(ViewState["TotalNumberSolAdded"] ?? 0); }
set { ViewState["TotalNumberSolAdded"] = value; }
}
private void AddMoreControls(int controlNumber, PlaceHolder plc,int n)
{
TextBox txtBoxMoreSolution = new TextBox();
Label lblMoreSolution = new Label();
btn = new Button();
btn.Text = "add step";
btn.ID = "btn" + controlNumber;
btn.Click += new EventHandler(btn_Click);
ViewState["btnId"] = btn.ID;
Label lbl = new Label();
lbl.Text = "soltuion" + (controlNumber + 1);
lbl.ID = "moreSolution" + (controlNumber + 1);
lbl.Font.Size = 20;
lbl.Font.Underline = true;
txtBoxMoreSolution.ID = "txtBoxMoreSolution" + controlNumber;
txtBoxMoreSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreSolution.Width = 470;
txtBoxMoreSolution.Height = 50;
lblMoreSolution.ID = "lblMoreSolution" + controlNumber;
lblMoreSolution.Text = "Step " + n + ": ";
lblMoreSolution.Width = 200;
plc.Controls.Add(lbl);
plc.Controls.Add(lblMoreSolution);
plc.Controls.Add(txtBoxMoreSolution);
plc.Controls.Add(btn);
}
private void btn_Click(object sender, EventArgs e)
{
TotalNumberSolStepAdded++;
AddMoreStepControls(TotalNumberSolStepAdded, plcAddMoreSolution);
}
protected int TotalNumberSolStepAdded
{
get { return (int)(ViewState["TotalNumberSolStepAdded"] ?? 0); }
set { ViewState["TotalNumberSolStepAdded"] = value; }
}
private void AddMoreStepControls(int controlNumber, PlaceHolder plc)
{
TextBox txtBoxMoreStepSolution = new TextBox();
Label lblMoreStepSolution = new Label();
txtBoxMoreStepSolution.ID = "txtBoxMoreStepSolution" + controlNumber;
txtBoxMoreStepSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreStepSolution.Width = 470;
txtBoxMoreStepSolution.Height = 50;
lblMoreStepSolution.ID = "lblMoreStepSolution" + controlNumber;
lblMoreStepSolution.Text = "Step " + (controlNumber +1) + ": ";
lblMoreStepSolution.Width = 200;
plc.Controls.Add(lblMoreStepSolution);
plc.Controls.Add(txtBoxMoreStepSolution);
}