Click here to Skip to main content
6,822,123 members and growing! (17,717 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Adding controls to asp.net dynamically and persisting them

By Varun Mahajan

Adding controls to asp.net dynamically and persisting them
VB8.0, Windows, .NET2.0, ASP.NET, WebForms, VS2005, Dev
Posted:5 Jul 2007
Views:12,150
Bookmarked:8 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.05 Rating: 2.43 out of 5
2 votes, 28.6%
1
2 votes, 28.6%
2

3
1 vote, 14.3%
4
2 votes, 28.6%
5

Introduction

I have seen a lot on articles on the web for generating controls dynamically in asp.net page and retaining them on postback. But all of the articles seems to be quite complex. I just wanted to make the whole thing somewhat simpler. So check if this seems to be a simple way out to do that.

I shoult tell you the steps involved in generating controls dynamically

  1. Add a Table/Div container to the aspx page
  2. Have a collection of contol IDs as a Viewstate-backed property in code-behind. I have used Generic List here as List(Of String)
  3. On the 'Add Control Button' click event's eventhandler, add the new ID to this list. Make sure that these IDs are unique
  4. On Page_PreRender generate the controls on the basis of these unique ids

Using the code

To add a control on a Postback event(Say, click of a button), we have to generate that control in code. And then add it to a container like div or table, which must be already present in the aspx page. Like CreateATableRow function in the code does that like:

Dim rowAdditional As New TableRow()
rowAdditional.ID = textName + "ROWID"

Dim cellAdditinal As New TableCell()
cellAdditinal.ID = textName + "CELLID"

Dim textAdditional As New TextBox()
textAdditional.ID = textName + "TEXTID"

textAdditional.Text = textName
''add textbox to celll

cellAdditinal.Controls.Add(textAdditional)
'add cell to row

rowAdditional.Controls.Add(cellAdditinal)

Return rowAdditional

All the previous added controls IDs are maintained as a generic list as List(Of String) so as to add them at page rendering. List is also beneficial because we have to check that the new control ID does not exist already, which can be done by checking Contains function of that list like:

AdditionalEnteries.Contains(textToBeAdded)

After that all the contol IDs existing in AdditionalEnteries list are just rendered on Page_PreRender.

For Each keyEntry As String In AdditionalEnteries
    If Not String.IsNullOrEmpty(keyEntry) Then
        tblAdditionalControls.Controls.Add(CreateATableRow(keyEntry))
    End If 
Next 

Things done!!! Happy Coding.

Points of Interest

Using of generic is optional here. But use of them make a lot of thing easier a lot of time, including this

History

  1. Modified the language to make the reading easier.

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

Varun Mahajan


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberGordon Boulton9:42 18 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jul 2007
Editor:
Copyright 2007 by Varun Mahajan
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project