<cc1:TabPanel ID="tabStart" runat="server" HeaderText="Start">
<ContentTemplate>
<asp:PlaceHolder ID="phControls" runat="server" />
<br />
<asp:Button ID="btnAddControl" runat="server" Text="Add a new text box"
CausesValidation="false" OnClick="AddDemoControl"/>
</ContentTemplate>
</cc1:TabPanel>
In the code behind of the page I have the Page_Load event and the event handler for the OnClick event of the ASP button.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
//Add 2 new textboxes to the specified placeholder
phControls.Controls.Add(new TextBox());
phControls.Controls.Add(new TextBox());
}
protected void AddDemoControl(object sender, EventArgs e)
{
phControls.Controls.Add(new TextBox());
}
When the page is loaded for the first time, 2 textbox controls are added to the page. When you click the button, only one textbox is shown. This is because the check to see if the page is a postback stops the first two textboxes from being loaded. Removing this line will mean that when you click the button three textboxes are shown on screen.
If you keep clicking the button, only three textboxes will be shown. This is because the controls do not survive the postback event and so are recreated new every time. So in this way this is one of the reasons I will use the repeater model to create dynamic controls.
How to do this using a basic textbox control will be covered in the next post.
No comments:
Post a Comment