Click here to Skip to main content
13,834,141 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 3 Dec 2012
Licenced MIT

How to Create Controls Dynamically in ASP.NET and Retrieve Values from It

, 3 Dec 2012
Rate this:
Please Sign up or sign in to vote.
How to create controls dynamically in ASP.NET and retrieve values from it

Sometimes, you may require to create controls dynamically in ASP.NET. Because of the ASP.NET page life cycle, you may lose the created controls on next postback (ASP.NET framework recreates the page on postback). You can resolve this issue by overriding LoadViewState() and SaveViewState() methods. LoadViewState() method restores view-state information from a previous page request that was saved by the SaveViewState method. This method will not be executed on the first time. SaveViewState() method saves any server control view-state changes that have occurred since the time the page was posted back to the server. So in SaveViewState() method, store the values you wish to create. And in the LoadViewState() method, retrieve from the viewstate, which returned from SaveViewState() method.

Here is a sample implementation, in this text boxes are created based on the drop down list selection. In the SaveViewState() method, the value of the drop down list stored to the view state, and in the LoadViewState(), controls are recreated using the viewstate information.

private void CreateDynamicControls(int count)
    for (int i = 0; i < count; i++)
        var textBox = new TextBox();
        var textboxCell = new TableCell();
        var tableRow = new TableRow();

protected override object SaveViewState()
    var viewState = new object[2];
    //Saving the dropdownlist value to the View State
    viewState[0] = int.Parse(ddlRecipients.SelectedValue); ;
    viewState[1] = base.SaveViewState();
    return viewState;

protected override void LoadViewState(object savedState)
    //Getting the dropdown list value from view state.
    if (savedState is object[] && ((object[])savedState).Length == 2)
        var viewState = (object[])savedState;
        var count = int.Parse(viewState[0].ToString());

protected void cmdSubmit_Click(object sender, EventArgs e)
    //To get the textbox value, you can loop through
    //the table cells and read the textbox controls
    foreach (TableRow row in tblRecipients.Rows)
        var textbox = row.Cells[0].Controls[0] as TextBox;

Happy programming! :)

You can find more information about LoadViewState() method and SaveViewState() method from MSDN.

Related Content

  1. How to persist checkbox state in gridview while paging
  2. How to prevent duplicate record insertion on Page refresh
  3. ArgumentException – This row already belongs to another table
  4. Accessing server side controls from JavaScript
  5. Selecting controls using LINQ


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Anuraj Parameswaran
Technical Lead
India India
Working as Tech. Lead

My blog :
You can follow me in twitter : @anuraj

You may also be interested in...


Comments and Discussions

AnswerHow to create controls dynamically in ASP.NET and retrieve values from it ? Pin
Member 976096529-Apr-14 1:09
memberMember 976096529-Apr-14 1:09 
QuestionNice article. Pin
dotnetpickles9-Feb-14 20:28
memberdotnetpickles9-Feb-14 20:28 
GeneralMy vote of 5 Pin
Debopam Pal18-Nov-13 6:45
professionalDebopam Pal18-Nov-13 6:45 
GeneralHelp needed for reterive the selected values of dynamically created combobox while postback on button click Pin
venu_thiru13-Nov-13 6:15
membervenu_thiru13-Nov-13 6:15 
GeneralRe: Help needed for reterive the selected values of dynamically created combobox while postback on button click Pin
Dhyanga9-Dec-14 11:32
memberDhyanga9-Dec-14 11:32 
GeneralMy vote of 5 Pin
Bizisto14-Oct-13 4:18
memberBizisto14-Oct-13 4:18 
Questionthank you! Pin
PhilPhan29-Apr-13 7:00
memberPhilPhan29-Apr-13 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190114.1 | Last Updated 3 Dec 2012
Article Copyright 2012 by Anuraj Parameswaran
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid