C dynamically generates DropDownList execution failure analysis

  • 2021-01-19 22:21:10
  • OfStack

This paper analyzes the reasons for the failure of C# dynamic generation DropDownList. Share with you for your reference. The details are as follows:

Today study DDL control dynamically generated when encounter a problem, when I found after click button writing the code Can only create a DDL control, and then click will not work, then I set the variable inside, take a look at this event is not implemented, found variable does not change, just a little puzzled, later found when I trigger other events, controls disappear! button click events, I think is returned, and new controls will disappear after back, I realized, originally have created new controls, every time every time just click button again comes back page, that generates before controls disappear, and that only 1 1 controls are generated by this time, I again query 1 asp life cycle It seems to understand the page lifecycle will not clear out a lot of problems.

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

1. Initialization (Init)
2. Control state loading (LoadControlState)
3. View loading (LoadViewState)
4. Return data processing (LoadPostData)
5. Page loading (OnLoad)
6. Rendering (OnPreRender)
7. Save view state (SaveViewState)
8. Save control state (SaveControlState)
9. Rendering (Render)

Here, when I click ES35en to trigger the new ES36en control event, this event is executed, but the control created last time did not save the state, so the above problem occurs
The method to save the state is simply studied, that is, to save the state to HiddenField


<body>
 <form id="form1" runat="server">
 <div>
  <asp:HiddenField ID="hidden" runat="server" />
  <asp:Button ID="Btn" runat="server" Text=" Dynamically create " 
   onclick="Btn_Click" />
  <asp:Button ID="GetValue" runat="server" Text=" Gets the selected value " 
   onclick="GetValue_Click" />
  <asp:Label ID="lbl" runat="server" Text=""></asp:Label>
  <asp:Panel ID="PanelControl" runat="server">
  </asp:Panel>
 </div>
 </form>
</body>

Below is a dynamically created code principle to click on the button Give HiddenField assignment or can give ViewState assignment and then create control, so that each time you enter the page again, whether iddenField or Viewstate have value, have showed that dynamically created controls, thus keep the status, when you click the other controls, both won't 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 postback  
{
}
}
protected void Btn_Click(object sender, EventArgs e)// Creating controls 
{
  Hidden.Value = "flag";// This says it's already been created 1 Control the 
  CreateDropDownList();
}

Next is the basic code to create ddl


private void CreateDropDownList()
{
  DropDownList ddl = new DropDownList();
  ddl.ID = "ddl";
  ddl.Items.Add(new ListItem("1", "1"));
  ddl.AutoPostBack = true;
  ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
  // to ddl Add event 
  PanelControl.Controls.Add(ddl); // Into the container 
}

The following values are selected for clicking on the control to get the dynamically created control, which is a postback


protected void GetValue_Click(object sender, EventArgs e)
{
  DropDownList ddl = PanelControl.FindControl("ddl") as DropDownList;
  if (ddl != null)
  {
   lbl.Text =" Clicking the button yields a value of "+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 its own postback event is "+ddl.SelectedValue;
  }
}

I hope this article is helpful to your C# program design.


Related articles: