Creating a dynamic control in ASP. NET is always not so smooth, especially when the life cycle of the page is not so smooth! Here we will briefly describe the requirements and provide a solution for you to see if there is any better idea. If there is one, it will be my pleasure!
Requirement: There is an add button on the page. Each time you click this button, a webpartzone is dynamically created on the page!
Reminder: webpartzone can only be created before oninit or. Otherwise, an exception is reported!
As we all know, the button clicking event is triggered at raisepostbackevent, which means that the clicking event is executed after the onload stage, far behind the oninit stage, and the viewstate is ready at the onload stage,Viewstate cannot be used for oninit and earlier stages.! If you try to create controls such as webpartzone in the button click event, the only consequence is that a page error occurs. If you create a control in oninit, because viewstate is not ready, some data, such as the number of files to be created (in viewstate), cannot be obtained!
At present, I have not found any good solutions for this problem. After experiment, I barely come up with a solution that is not very elegant.Use hiddenfield to save the data, and then directly use request. Form ["XXX"] to retrieve the data in the oninit stage. The result of judging whether to click the button is also determined by whether the request. form has the corresponding data.! Let's take a look.CodeRight!
<% @ 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" /> & Nbsp;
< 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 );
// Obtain the number of previously created controls
If ( ! String. isnullorempty ( This . Request [ " Hfcount " ])
{
_ Count=Convert. toint32 (This. Request ["Hfcount"]);
}
// If you press the "add" button, Count Plus One
String Target = This . Request [ " Btnadd " ];
If (Target = " Add " )
{
_ Count++;
}
// Dynamically create controls
For ( Int I = 0 ; I < _ Count; I ++ )
{ // Here we take textbox as an example. What we need to create is 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)
{
//The webpartzone control cannot be added here. It must be oninit or earlier. Otherwise, an exception is reported.
}
}
I hope you can come up with better solutions @_@