Hi micah,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ListBox ID="lbCategories" CssClass="form-control dual_select" runat="server" multiple=""
AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged"></asp:ListBox>
<br />
<asp:Button Text="Add" runat="server" OnClick="OnAdd" />
<br /><br />
<asp:Panel ID="pnlDynamic" runat="server"></asp:Panel>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i);
i++;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CategoryID,CategoryName FROM Categories"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
lbCategories.DataSource = cmd.ExecuteReader();
lbCategories.DataValueField = "CategoryID";
lbCategories.DataTextField = "CategoryName";
lbCategories.DataBind();
con.Close();
}
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
int index = lbCategories.SelectedIndex;
if (pnlDynamic.FindControl("txtDynamic" + (index + 1)) != null)
{
(pnlDynamic.FindControl("txtDynamic" + (index + 1)) as TextBox).Text = lbCategories.SelectedItem.Text;
}
}
protected void OnAdd(object sender, EventArgs e)
{
int index = pnlDynamic.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index);
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txt.CssClass = "form-control";
pnlDynamic.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
pnlDynamic.Controls.Add(lt);
}
VB.Net
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox("txtDynamic" & i)
i += 1
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CategoryID,CategoryName FROM Categories")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
lbCategories.DataSource = cmd.ExecuteReader()
lbCategories.DataValueField = "CategoryID"
lbCategories.DataTextField = "CategoryName"
lbCategories.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = lbCategories.SelectedIndex
If pnlDynamic.FindControl("txtDynamic" & (index + 1)) IsNot Nothing Then
TryCast(pnlDynamic.FindControl("txtDynamic" & (index + 1)), TextBox).Text = lbCategories.SelectedItem.Text
End If
End Sub
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = pnlDynamic.Controls.OfType(Of TextBox)().ToList().Count + 1
Me.CreateTextBox("txtDynamic" & index)
End Sub
Private Sub CreateTextBox(ByVal id As String)
Dim txt As TextBox = New TextBox()
txt.ID = id
txt.CssClass = "form-control"
pnlDynamic.Controls.Add(txt)
Dim lt As Literal = New Literal()
lt.Text = "<br />"
pnlDynamic.Controls.Add(lt)
End Sub
Screenshot