Display selected ListBox item in dynamically created TextBox using C# and VB.Net in ASP.Net

micah
 
on Sep 19, 2021 10:58 PM
520 Views

How do i click names inside lisbox and this play the names in generated textbox. I have names coming from database and been displayed inside listbox, then my challenge is that i want to click each name and it will be displayed on each generated textbox.

<asp:ListBox ID="ddlbrand" CssClass="form-control dual_select" runat="server" multiple="">
    <asp:ListItem ></asp:ListItem>
</asp:ListBox>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string Constr1 = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        using (SqlConnection CONE = new SqlConnection(Constr1))
        {
            using (SqlCommand C0MDE = new SqlCommand("SELECT C_ID,Brand FROM Stock_Table"))
            {
                // C0MDE.Parameters.AddWithValue("@Brand", this.ddlbrand.SelectedValue.Trim());
 
                // C0MDE.Parameters.AddWithValue("@UserName", Session["userName"]);
                C0MDE.CommandType = CommandType.Text;
                C0MDE.Connection = CONE;
                CONE.Open();
                ddlbrand.DataSource = C0MDE.ExecuteReader();
                ddlbrand.DataValueField = "C_ID";
                ddlbrand.DataTextField = "Brand";
                // ddlProductCode.DataValueField = "UserName";
 
                ddlbrand.DataBind();
                CONE.Close();
            }
        }
        // ddlbrand.Items.Insert(0, new ListItem("--Select Brand--", "0"));
    }
}
 
protected void btnAdd_Click(object sender, EventArgs e)
{
}
 
public string ControlRenderer(Control control)
{
    StringWriter writer = new StringWriter();
    control.RenderControl(new HtmlTextWriter(writer));
    return writer.ToString();
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 21, 2021 12:21 AM

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