Today, I encountered a problem when studying the dynamic generation of ddl controls. When I wrote the code, I found that only one ddl control can be created after clicking the button, and then clicking it has no effect.Later I set variables in it,See if this event is not implemented,Found that the variables have not changed,Just a little puzzled,I later discovered that when I triggered other events,The controls are gone! I think the button's click event will be passed back,The newly created control will disappear after the postback,I understandIt turns out that new controls are created every time.Just return the page every time the button is clicked again,Caused the previously generated controls to disappear,And the only control is generated this time,I checked the life cycle of asp again. It seems that the understanding of the life cycle of the page is not clear and there will be many problems.

Simply put, the life cycle of the page is divided into the following processes


2.Control state loading (loadcontrolstate)

3 view loading (loadviewstate)

4. Postback data processing (loadpostdata)

5. page load (onload)

6. rendering (onprerender)

7. Save view state

8. Save the control state (savecontrolstate)

9. render

Here when I click the button to trigger the event of the new ddl control,This event is executed,But the control created last time does not save state,So the above problem appeared

A brief study of the method of saving state,Is to save the state to hiddenfield

 <form runat="server">
  <asp:hiddenfield runat="server" />
  <asp:button runat="server" text="dynamic creation"
   onclick="btn_click" />
  <asp:button runat="server" text="Get selected value"
   onclick="getvalue_click" />
  <asp:label runat="server" text=""></asp:label>
  <asp:panel runat="server">

The following is the principle of dynamically creating code. Click the button to assign a value to the hiddenfield or you can assign a value to the viewstate and then create a control.So every time you enter the page,Determine if iddenfield or viewstate has a value,Display the dynamically created control when there is,This saves the state,When you click on another control,Neither will disappear again.

protected void page_load (object sender, eventargs e)
if (! string.isnullorempty (hidden.value)&&hidden.value == "flag")
   createdropdownlist ();
if (! ispostback) //Create control cannot be written here,This is to determine whether it is a postback
protected void btn_click (object sender, eventargs e) //Create control
  hidden.value="flag";//This indicates that a control has been created
  createdropdownlist ();

Next is the basic code to create ddl

private void createdropdownlist ()
  dropdownlist ddl=new dropdownlist ();
  ddl.items.add (new listitem ("1", "1"));
  ddl.selectedindexchanged +=new eventhandler (ddl_selectedindexchanged);
  //Add events to ddl
  panelcontrol.controls.add (ddl);//put in the container

The following is to click the control to get the selected value of the dynamically created control.

protected void getvalue_click (object sender, eventargs e)
  dropdownlist ddl=panelcontrol.findcontrol ("ddl") as dropdownlist;
  if (ddl!=null)
   lbl.text="click the button to get the value" + ddl.selectedvalue;

Dynamically created ddl events

protected void ddl_selectedindexchanged (object sender, eventargs e)
  dropdownlist ddl=sender as dropdownlist;
  if (ddl!=null)
   lbl.text="The value obtained through the postback event itself" + ddl.selectedvalue;
  • Previous PHP output calendar code example
  • Next Python implementation to set windows desktop wallpaper code sharing