2016-04-21 4 views
0

2everyone! Ich brauche Ihre Hilfe .. Ich möchte statische Zeilen mit Schaltfläche klicken Ereignis in Page_Load Methode (CreateChildControls) erstellen. Wenn ich auf "Erstellen" и die gleiche Zeile unter dem bestehendenASP.net - C# Steuerelemente ausblenden/anzeigen, wenn auf die Schaltfläche geklickt wird

T sein ist meine UI visualisieren möchten: enter image description here Dies ist mein Code:

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

Wie Taste überprüfen geklickt wird? – Gohyu

+0

Meine Lösung funktioniert nicht; – Gohyu

+0

Verwenden Sie Datenbindung mit DataGrid –

Antwort

1

Fügen Sie den Code hinzufügen eine neue Zeile in den Event-Handler, anstatt es in CreateChildControls der Verwendung:

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

Like this Sie hinzufügen neue Zeile, wenn auf die Schaltfläche geklickt wird, und müssen keine boolesche Variable btn1WasClicked verwenden.

0

diiN_ stimmt einfach Ihren gesamten Code in Seite stellen, wenn (btn1WasClicked) unter 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

Wenn ich auf btn1_Click klicke, laden Sie die Seite neu und laden Sie die Methode CreateChildControls() .... – Gohyu

Verwandte Themen