2017-03-02 216 views
2

我正在用ASP.net創建應用程序。我有一個按鈕,點擊時會生成一些html,一些asp文本框和一個asp按鈕(第二個按鈕)。這可以正常工作,據我所知。現在我想要的是,當我點擊那個新創建的按鈕時,我希望它創建一些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>")); 
    } 
} 

}

+1

請閱讀[問]並分享您的代碼。 – CodeCaster

回答

1

這裏是連續地添加按鈕的頁面的示例。每個新創建的按鈕還可以在頁面上添加另一個按鈕。這可以用於所有類型的控件。 此外,你將不得不重新創建每個回發的動態控制,否則他們和他們的內容將會丟失。

protected void Page_Load(object sender, EventArgs e) 
{ 
    //set the initial number of buttons 
    int buttonCount = 1; 

    //check if the viewstate with the buttoncount already exists (= postback) 
    if (ViewState["buttonCount"] != null) 
    { 
     //convert the viewstate back to an integer 
     buttonCount = Convert.ToInt32(ViewState["buttonCount"]); 
    } 
    else 
    { 
     ViewState["buttonCount"] = buttonCount; 
    } 

    //create the required number of buttons 
    for (int i = 1; i <= buttonCount; i++) 
    { 
     createButton(i); 
    } 
} 

private void createButton(int cnt) 
{ 
    //create a new butotn control 
    Button button = new Button(); 
    button.Text = "Add another Button (" + cnt + ")"; 

    //add the correct method to the button 
    button.Click += DynamicButton_Click; 

    //another control, in this case a literal 
    Literal literal = new Literal(); 
    literal.Text = "<br>"; 

    //add the button and literal to the placeholder 
    PlaceHolder1.Controls.Add(button); 
    PlaceHolder1.Controls.Add(literal); 
} 

protected void DynamicButton_Click(object sender, EventArgs e) 
{ 
    //get the current number of buttons 
    int buttonCount = Convert.ToInt32(ViewState["buttonCount"]) + 1; 

    //create another button 
    createButton(buttonCount); 

    //set the new button count into the viewstate 
    ViewState["buttonCount"] = buttonCount; 
} 

UPDATE

您也可以委派Command的按鈕,而不是一個Click,以及與此您可以用按鈕作爲CommandArgument一起發送的變量。你將不得不改變一下按鈕的創建。

//add the correct method to the button 
button.Command += DynamicButton_Command; 

//now you can also add an argument to the button 
button.CommandArgument = "Create Hazard"; 

您還需要一個不同的方法來處理按鈕點擊。

protected void DynamicButton_Command(object sender, CommandEventArgs e) 
{ 
    //get the current number of buttons 
    int buttonCount = Convert.ToInt32(ViewState["buttonCount"]) + 1; 

    //create another button 
    createButton(buttonCount); 

    //set the new button count into the viewstate 
    ViewState["buttonCount"] = buttonCount; 

    //get the commandargument from the button 
    string buttonArgument = e.CommandArgument.ToString(); 
} 
+0

非常感謝,我試了一下。 –

+0

嘿,我一直在玩你給我的代碼,它的相當不錯。我想知道,是否可以這樣做,當我點擊'Are/Equipment Inpected'框3或4中的「創建危險」按鈕時,Hazard行出現在單擊的相應文本框中?我會添加一張圖片,希望能更好地解釋。非常感謝您的指導。 https://i.gyazo.com/a63c828956415de1d83b1828a07c740f.png –

+0

是的,這是可能的。查看我的更新。您可以從'Click'切換到'Command'以隨同按鈕一起發送一個變量。 – VDWWD