Click here to Skip to main content
Click here to Skip to main content

Dynamic generate control according to the user's operation

By , 18 Sep 2007
 
Screenshot - demo.gif

Introduction

sometimes we need to dynamic generate controls according to the user's operation, such as in a button click event. However, after the post back, the dynamic generated controls would disappear. I use viewstate to keep the infomation of the auto generated controls and generated them at the page load. Background

Using the code

Unfortunately the WebControl class is unserializable, so I define a serializable class to store the info of dynamic generated controls

 [Serializable]
    public class ContactControl
However, the genereted controls infomation are stored in the viewstate.
 private List<ContactControl> ContactControls
        {
            get
            {
                return ReadViewState<List<ContactControl>>("AddedControls", new List<ContactControl>());
            }
            set
            {
                ViewState["AddedControls"] = value;
            }
        }
At every page load, get the genereted controls information and generate them.
    protected override void OnLoad(EventArgs e)
        {
            List<ContactControl> contactControls = ContactControls;
            foreach (ContactControl contactControl in contactControls)
            {
                GenerateControl(contactControl);
            }
        }

Points of Interest

Although it can works, I believe it was not the best solution. Please email me if some one have a better solution

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

bluelzdwy
China China
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Sep 2007
Article Copyright 2007 by bluelzdwy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid