This article shows how to create dynamic controls in ASP.Net and how to read the dynamically created control values using ASP.NET.
I have searched for and found many websites but they do not show how to read values of dynamically created controls, only how to create dynamic controls in ASP.Net.So today I will show to how to create dynamic controls in ASP.Net and how to read the dynamically created control values using ASP.NET. In this article, I have dynamically created "TextBox", "DropDownList", "RadioButtonList" and "CheckBoxList" contorls.For better understand, please download the source code form this article.The following shows how to to create a dynamic TextBox and read its value..ASPX Code
C# CodeRead TextBox ValueExampleThe following shows how to to create a dynamic DropDownList and read its value..ASPX CodeC# Code
Read DropDownList Value
Assign SelectedIndexChange Event to DroupDownListAdd the "ddl_SelectedIndexChanged" event to the droupdownlist dynamically using the following code:ExampleThe following shows how to to create a dynamic RadioButtonList and read its value..ASPX CodeC# CodeRead RadioButtonList ValueExampleCreate dynamic CheckBoxList and read their value:.ASPX CodeC# CodeRead CheckBoxList ValueExampleDynamically Created Controls Losing Data After Post-back or Retaining State for Dynamically Created Controls in ASP.NET ApplicationsBefore we proceed, we need to keep in mind one thing. When you create a dynamic control in a page and if you do a post-back then the created control will be lost. I mean to say that "When using dynamic controls, you must remember that they will exist only until the next post-back. ASP.NET will not re-create a dynamically added control". You need to recreate all dynamically created controls on every post-back (in load event at the latest). You also need to ensure that they get the same ID as before to trigger events and maintain View State.To show this issue I have used the following code:
ASP.NET GridView Control Pocket Guide