Dynamically creating a control in asp.net is always less smooth, especially if the life cycle of the page is not so clear! Here a simple description of the requirements, and then provide a solution, we see if there is no better idea, if there is my great fortune, hehe!
Requirements: There is an Add button on the page, each click on the button, dynamically create a webpartzone! on the page
Reminder: WebPartZone can only be created before oninit or the report is abnormal!
As we all know, button Click events are triggered when raisepostbackevent, which means that the click event is executed after the onload phase, far behind the OnInit stage, and ViewState is ready when the onload OnInit and the previous stage simply cannot use viewstate! If you try to create a WebPartZone control in the button click event, the only consequence is a page error, and if you create the control inside the OnInit, because ViewState is not ready, Then some data, such as the number of current needs to be created (inside viewstate) can not be obtained!
At present, I have not found any good solution to this problem, after the experiment, reluctantly came up with a less elegant solution is to use HiddenField to save data, and then directly use request.form["XXX"] in the OnInit phase to obtain data and determine whether the Click button is also through the Request.Form whether there is a corresponding data to judge! Nonsense don't say much, everybody look code!
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head runat= "Server" >
<title>untitled page</title>
</head>
<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server"/>
<div>
<asp:placeholder id= "PlaceHolder1" runat= "Server" ></asp:PlaceHolder>
<br/>
<asp:button id= "Btnadd" runat= "Server" text= "Add" onclick= "btnAdd_Click"/>
<asp:hiddenfield id= "Hfcount" runat= "server" value= "0"/>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
Using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _default:system.web.ui.page
{
private int _count = 0;
protected override void OnInit (EventArgs e)
{
base. OnInit (e);
//Get the number of controls previously created
if (! String.IsNullOrEmpty (this. request["Hfcount"])
{
_count = Convert.ToInt32 (this. request["Hfcount"]);
}
//If you press the "ADD" button, Count plus a
string target = this. request["Btnadd"];
if (target = = "Add")
{
_count++;
}
//Dynamically creating Controls
for (int i = 0; i < _count; i++)
{//Here in the case of a textbox, you actually need to create a WebPartZone
textbox Newtextbox = new TextBox ();
newtextbox.id = "TXT" + i.tostring ();
this. PLACEHOLDER1.CONTROLS.ADD (Newtextbox);
}
}
protected void Page_Load (object sender, EventArgs e)
{
Hfcount.value = _count. ToString ();
}
protected void btnAdd_Click (object sender, EventArgs e)
{
//Cannot add WebPartZone control here, only oninit or before, otherwise report an exception
}
}