ASP.net - 单击按钮时的C#隐藏/显示控件

时间:2016-04-21 07:22:56

标签: c# asp.net

2everyone!我需要你的帮助.. 我想在Page_Load方法(CreateChildControls)中使用按钮单击事件创建静态行。当我点击“创建”时,想要在现有的

下可视化同一行

T 他是我的用户界面 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;
    }
}

2 个答案:

答案 0 :(得分:1)

添加代码以向事件处理程序添加新行,而不是在CreateChildControls中使用它:

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

像这样,您可以在单击按钮时添加新行,而不必使用布尔变量btn1WasClicked

答案 1 :(得分: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>"));
}