Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday 3 April 2015

Introduction:
In this article I will explain how to add or insert it into Listbox in asp.net using  c# and vb.net or on button click.

Sometime while development it is required to insert data into textbox on button click. So in this article I have insert data in listbox from textbox on button click. I have enter value in textbox, then click button, value will be entered into listbox.

Source code:

Design section: add web-page to impement the concept. And design page as given below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 <fieldset style="width:300px">
    <legend><strong>Listbox Example</strong></legend>
   <div>
Enter Text: <asp:TextBox ID="txtname" runat="server"></asp:TextBox><br /><br />
<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>
    </fieldset>
    </div>
    </form>
</body>
</html>


Asp.Net Code(C#):

Write the following code in code behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class listbox : System.Web.UI.Page
{
    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 = "";
    }
}

Asp.Net Code(Vb.NET):

Write the following code in code behind file:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data

Partial Public Class listbox
    Inherits System.Web.UI.Page
    Dim 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 DataTable = 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 = txtname.Text.Trim()
        dt = CType(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
Categories: , ,

0 comments:

Post a Comment