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
T his is my UI: 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;
}
}
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
.
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>"));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.