简体   繁体   中英

Dynamically Adding More Controls to ASP .NET Page?

I am using System.Web.UI.WebControls.Panel to hold a FormTable . In FormTable I am adding more Controls on PostBack event and then add it back to System.Web.UI.WebControls.Panel . In this case I do not want to lose the states of existing controls, and want to add more controls to FormTable . Which will again be assigned to System.Web.UI.WebControls.Panel .

If I clear System.Web.UI.WebControls.Panel and then add new FormTable (with a few more added controls in it now) then this will work fine but the states of Controls will be lost.

I need to add more controls, keeping states of existing controls intact. How can I achieve it?

Eg.

Panel->FormTable is Holding

TextBox1 : 1
TextBox2 : 2

Dynamically on a button click I add one more TextBox to it:

TextBox1 : "1"
TextBox2 : "2"
TextBox3 : "3"

But for adding this 3rd TextBox3 I will have to clear the FormTable otherwise it will throw the exception relating to the Controls' IDs, saying that previous Control States does not match the States that you are assigning this time.

If I do clear System.Web.UI.WebControls.Panel and then added new FormTable then TextBoxes will be added but their states will be lost and output will be like this:

TextBox1 : ""
TextBox2 : ""
TextBox3 : ""

>> I want to add more info to it. While dynamically adding new Items to my FormTable. I will be adding these new Items any where in the table so the Control's ID will no longer match the previously created Controls' ID in the FormTable.

Eg at position 4 in FormTable I can dynamically insert a HyperLink where previously was a TextBox. Shifting TextBox to position 5.

Wrap all the content you want to keep state inside an Updatepanel to be partially rendered without a postback. :

  <asp:ScriptManager ID="ScriptManager1" 
                               runat="server" />
            <asp:UpdatePanel ID="UpdatePanel1"
                             runat="server">
                <ContentTemplate>
                           //content
                </ContentTemplate>
           </asp:UpdatePanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM