ASP.netでアプリケーションを作成しています。私はボタンをクリックすると、いくつかのHTML、いくつかのASPのテキストボックスとaspボタン(2番目のボタン)を生成します。これは私が理解できる限りうまく動作します。今、私が望むのは、その2番目の、新しく作成されたボタンをクリックすると、いくつかのhtml + asp.netテキストボックスを作成したいのです。動的に作成されたASP.netコントロール内でASP.netコントロールを動的に作成する方法
これは私には分かりにくいようですが、簡単な方法がありますか?私はそれを把握することができない、私はボタン2のonclickイベントで作成するが、それはまだ存在しません。
ありがとうございます。
何が起こっているのかを確認したい場合に備えて、コードを見るのが少し簡単かもしれないと思っています。ここ
namespace ConnorMackayWebForm
{
public partial class InspectionCreate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Set the initial amount of areas and hazards
int areaCount = 0;
int hazardCount = 0;
//Check if the viewstate with the area count already exists
if (ViewState["areaCount"] != null)
{
//Convert the view state back to an int
areaCount = Convert.ToInt32(ViewState["areaCount"]);
}
else
{
ViewState["areaCount"] = areaCount;
}
//Check if the viewstate with the hazard count already exists
if (ViewState["hazardCount"] != null)
{
//Convert the view state back to an int
hazardCount = Convert.ToInt32(ViewState["hazardCount"]);
}
else
{
ViewState["hazardCount"] = hazardCount;
}
//Create the required number of areas
for (int i = 1; i <= areaCount; i++)
{
createArea(i);
}
//Create the required number of hazards
for (int i = 1; i <= hazardCount; i++)
{
createHazard(i);
}
}
protected void btnCreateArea_Click(object sender, EventArgs e)
{
//Get the current number of areas
int areaCount = Convert.ToInt32(ViewState["areaCount"]) + 1;
//Create the area
createArea(areaCount);
//Set the new area into the viewstate
ViewState["areaCount"] = areaCount;
}
protected void btnCreateHazard_Click(object sender, CommandEventArgs areaCount)
{
//Get the current number of areas
int hazardCount = Convert.ToInt32(ViewState["hazardCount"]) + 1;
//Get the argument from the button
int placeholderID = Convert.ToInt32(areaCount.CommandArgument);
//Create the hazard
createHazard(hazardCount, placeholderID);
//Set the new hazard into the viewstate
ViewState["hazardCount"] = hazardCount;
}
private void createArea(int areaCount)
{
//Start generating the HTML
pnlArea.Controls.Add(new LiteralControl("<div class='box'>"));
pnlArea.Controls.Add(new LiteralControl("<div class='box-header with-border'>"));
pnlArea.Controls.Add(new LiteralControl("<h2 class='box-title'>Area/Equipment Inspected 1: "));
//Create the title dropdown
DropDownList ddArea = new DropDownList();
ddArea.ID = "ddArea" + areaCount;
pnlArea.Controls.Add(ddArea);
//Create the Other textbox in the title
pnlArea.Controls.Add(new LiteralControl(" Other: "));
TextBox txtOther = new TextBox();
txtOther.ID = "txtOther" + areaCount;
pnlArea.Controls.Add(txtOther);
//Generate HTML for the box body and begining of first column
pnlArea.Controls.Add(new LiteralControl("<div class='box-tools pull-right'>"));
pnlArea.Controls.Add(new LiteralControl("</div>"));
pnlArea.Controls.Add(new LiteralControl("</div>"));
pnlArea.Controls.Add(new LiteralControl("<div class='box-body'>"));
//Placeholder to put future hazards into
PlaceHolder phHazard = new PlaceHolder();
phHazard.ID = "phHazard" + areaCount;
pnlArea.Controls.Add(phHazard);
//Create hazard button
pnlArea.Controls.Add(new LiteralControl("<br>"));
Button btnCreateHazard = new Button();
//btnCreateHazard.Click += btnCreateHazard_Click;
btnCreateHazard.ID = "btnCreateHazard" + areaCount;
btnCreateHazard.Text = "Create Hazard";
btnCreateHazard.CssClass = "form-control";
//Pass along the current area count, allowing the onclick to pick it up, pass it to
//the create hazard method. That method will then take the argument and search for a place
//holder with matching ID and assign the controls to that placeholder.
btnCreateHazard.Command += btnCreateHazard_Click;
btnCreateHazard.CommandArgument = areaCount.ToString();
pnlArea.Controls.Add(btnCreateHazard);
pnlArea.Controls.Add(new LiteralControl("</div>"));
pnlArea.Controls.Add(new LiteralControl("</div>"));
}
private void createHazard (int hazardCount, int placeholderID)
{
//The starting of the HTML rows, etc
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<div class='row'>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<div class='col-lg-3'>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("Hazard: "));
//Create the Hazard Dropdown
DropDownList ddHazard = new DropDownList();
ddHazard.ID = "ddHazard" + hazardCount;
FindControl("phHazard" + placeholderID).Controls.Add(ddHazard);
//HTML ending the first column, starting second
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("</div>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<div class='col-lg-3'>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("Hazard Description: "));
//Create the hazard description textbox
TextBox txtHazardDesc = new TextBox();
txtHazardDesc.ID = "txtHazardDesc" + hazardCount;
FindControl("phHazard" + placeholderID).Controls.Add(txtHazardDesc);
//HTML ending second column, starting third
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("</div>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<div class='col-lg-3'>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("Corrective Action Due Date: "));
//Create the due date textbox
TextBox txtDueDate = new TextBox();
txtDueDate.ID = "txtDueDate" + hazardCount;
FindControl("phHazard" + placeholderID).Controls.Add(txtDueDate);
//HTML ending the third column, starting fourth
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("</div>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<div class='col-lg-3'>"));
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("Corrective Action Description: "));
//Create the corrective action description text box
TextBox txtActionDesc = new TextBox();
txtActionDesc.ID = "txtActionDesc" + hazardCount;
FindControl("phHazard" + placeholderID).Controls.Add(txtActionDesc);
//End the row
FindControl("phHazard" + placeholderID).Controls.Add(new LiteralControl("<br>"));
}
}
}
[お問い合わせ]を読んでコードを共有してください。 – CodeCaster