2016-04-21 88 views
0

2everyone!我需要你的幫助.. 我想在Page_Load方法(CreateChildControls)中使用按鈕單擊事件來創建靜態行。當我點擊「創建」и希望顯示在現行ASP.net - 隱藏/顯示控件按鈕被點擊時

牛逼他在同一行是我的UI: enter image description here 這是我的代碼:

public class HelloWorldWeb : WebPart 
{ 
    private TextBox txt11; 
    private DateTimeControl dt11; 
    private DateTimeControl dt12; 
    private TextBox txt12; 
    private TextBox txt13; 
    private Button btn1; 

    private TextBox txt21; 
    private DateTimeControl dt21; 
    private DateTimeControl dt22; 
    private TextBox txt22; 
    private TextBox txt23; 
    private Button btn2; 

    //private TextBox txt31; 
    //private DateTimeControl dt31; 
    //private DateTimeControl dt132; 
    //private TextBox txt32; 
    //private TextBox txt33; 
    //private Button btn3; 

    protected override void CreateChildControls() 
    { 

     txt11 = new TextBox(); 
     txt12 = new TextBox(); 
     txt13 = new TextBox(); 
     dt11 = new DateTimeControl(); 
     dt11.DateOnly = true; 
     dt12 = new DateTimeControl(); 
     dt12.DateOnly = true; 
     btn1 = new Button(); 
     btn1.Text = "Create"; 
     btn1.Click += new EventHandler(btn1_Click); 


     this.Controls.Add(new LiteralControl("<table class='ms-formbody' vAlign='top' >")); 

     this.Controls.Add(new LiteralControl("<tr>")); 
     this.Controls.Add(new LiteralControl("<td width='100' >")); 
     this.Controls.Add(txt11); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("<td width='100'>")); 
     this.Controls.Add(dt11); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("<td width='100'>")); 
     this.Controls.Add(dt12); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("<td width='100'>")); 
     this.Controls.Add(txt12); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("<td width='100'>")); 
     this.Controls.Add(txt13); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("<td width='100'>")); 
     this.Controls.Add(btn1); 
     this.Controls.Add(new LiteralControl("</td>")); 
     this.Controls.Add(new LiteralControl("</tr>")); 

     if (btn1WasClicked) 
     { 
      this.Controls.Add(new LiteralControl("<tr>")); 
      this.Controls.Add(new LiteralControl("<td width='100' >")); 
      this.Controls.Add(txt21); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("<td width='100'>")); 
      this.Controls.Add(dt21); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("<td width='100'>")); 
      this.Controls.Add(dt22); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("<td width='100'>")); 
      this.Controls.Add(txt22); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("<td width='100'>")); 
      this.Controls.Add(txt23); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("<td width='100'>")); 
      this.Controls.Add(btn2); 
      this.Controls.Add(new LiteralControl("</td>")); 
      this.Controls.Add(new LiteralControl("</tr>")); 
     } 

     this.Controls.Add(new LiteralControl("</table>")); 

     base.CreateChildControls(); 
    } 


    private bool btn1WasClicked = false; 

    private void btn1_Click(object sender, EventArgs e) 
    { 
     btn1WasClicked = true; 
    } 
} 
+0

下如何點擊查看按鈕? – Gohyu

+0

我的解決方案不工作; – Gohyu

+0

使用數據綁定,使用DataGrid –

回答

1

代碼添加到添加新行的事件處理程序,而不是使用它CreateChildControls

private void btn1_Click(object sender, EventArgs e) 
{ 
    // Add a new row 
} 

這樣你就可以添加點擊該按鈕時的新行,並且不必使用布爾變量btn1WasClicked

0

diiN_是正確的只是把側你的整個代碼,如果(btn1WasClicked)btn1_Click

private void btn1_Click(object sender, EventArgs e) 
{ 
// Add a new row 
    this.Controls.Add(new LiteralControl("<tr>")); 
    this.Controls.Add(new LiteralControl("<td width='100' >")); 
    this.Controls.Add(txt21); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("<td width='100'>")); 
    this.Controls.Add(dt21); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("<td width='100'>")); 
    this.Controls.Add(dt22); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("<td width='100'>")); 
    this.Controls.Add(txt22); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("<td width='100'>")); 
    this.Controls.Add(txt23); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("<td width='100'>")); 
    this.Controls.Add(btn2); 
    this.Controls.Add(new LiteralControl("</td>")); 
    this.Controls.Add(new LiteralControl("</tr>")); 
} 
+0

當我點擊btn1_Click,頁面重新加載並加載方法CreateChildControls().... – Gohyu

相關問題