0

2everyone! I need your help.. I want to create static rows with button click event in Page_Load method(CreateChildControls). When I click "Create" и want to visualize the same row under the existing

This is my UI: enter image description here This is my 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;
    }
}
5
  • How to check button is clicked? Commented Apr 21, 2016 at 7:26
  • My solution doesnt working; Commented Apr 21, 2016 at 7:26
  • Use databinding, with DataGrid Commented Apr 21, 2016 at 9:24
  • Please give me more info for this soluton... Commented Apr 21, 2016 at 9:26
  • It's hard to answer precisely because the subject si very complex. You have to follow some tutorial related to databinding. You allso have to understand the page lifecycle - and the stateless nature of html pages. You can't "persist" controls creation across postback, because each time the page load, the page is rebuilt from scratch. That's the point of databinding... feed databound control with datasource. Commented Apr 21, 2016 at 10:25

2 Answers 2

1

Add the code to add a new row to the event handler instead of using it in CreateChildControls:

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

Like this you can add a new row when the button is clicked and don't have to use a boolean variable btn1WasClicked.

Sign up to request clarification or add additional context in comments.

Comments

0

diiN_ is right just put your whole code in side if(btn1WasClicked) under 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>"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.