Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Add (Send) TextBox Values to ListBox in ASP.Net on Button Click using C#, VB.NET

Feb 21, 2015
Introduction:

Here I will explain how to add or send textbox values to listbox in
asp.net  on button click using c#, vb.net or bind textbox values to listbox on button click in asp.net using c#, vb.net with example.

Description:

In previous post I explained convert currency or numbers to words in asp.net, angularjs bind dropdownlist with text and value,
jQuery tag cloud example in asp.net, sitemap control example, send html page as mail body in asp.net, how to send mail with attachment in asp.net and many more articles related to asp.net using c#, vb.net. Now I will explain how to add or send textbox values to listbox in asp.net  on button click using c#, vb.net with example.

To add textbox values to listbox in asp.net  on button click using c#, vb.net we need to write aspx code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add Textbox values to Listbox items on button click </title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Text: <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add to Listbox" OnClick="btnAddClick" /> <br /> <br />
Listbox Values:  <asp:ListBox ID="lstdetails" runat="server"></asp:ListBox>
</div>
</form>
</body>
</html>
After completion of aspx page write the add following namespaces in codebehind

C# Code


using System;
using System.Data;
VB.NET Code


Imports System.Data
After completion of adding namespaces you need to write the code like as shown below

C# Code


DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["Details"] == null)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
ViewState["Details"] = dataTable;
}
}
}
protected void btnAddClick(object sender, EventArgs e)
{
string str = txtname.Text.Trim();
dt = (DataTable)ViewState["Details"];
dt.Rows.Add(str);
ViewState["Details"] = dt;
lstdetails.DataSource = dt;
lstdetails.DataTextField = "Name";
lstdetails.DataValueField = "Name";
lstdetails.DataBind();
txtname.Text = "";
}
VB.NET Code


Imports System.Data
Partial Class VBCode
Inherits System.Web.UI.Page
Private dt As DataTable = Nothing
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
If ViewState("Details") Is Nothing Then
Dim dataTable As New DataTable()
dataTable.Columns.Add("Name")
ViewState("Details") = dataTable
End If
End If
End Sub
Protected Sub btnAddClick(ByVal sender As Object, ByVal e As EventArgs)
Dim str As String
str = txtname.Text.Trim()
dt = DirectCast(ViewState("Details"), DataTable)
dt.Rows.Add(str)
ViewState("Details") = dt
lstdetails.DataSource = dt
lstdetails.DataTextField = "Name"
lstdetails.DataValueField = "Name"
lstdetails.DataBind()
txtname.Text = ""
End Sub
End Class
Demo


Add (Send) TextBox Values to ListBox in ASP.Net on Button Click using C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

www.code-sample.com said...

Awesome!!

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.