Add ListBox Items to ListView control in ASP.Net

Last Reply on Dec 25, 2013 02:02 AM By Azim

Posted on Dec 24, 2013 11:04 PM

Run mode as follows


In Listbox as follows
Candidatename


Ashiwn
Baskar
Chandru
Das
Gopal

Listview (control)
 
i want to display the above Listbox items into Listview.
 
for that how can i do in asp.net using c#.
Regards,
Narasiman p.

Posted on Dec 25, 2013 02:02 AM

This Way:

<form id="form1" runat="server">
    <div>
        <asp:ListBox ID="lbDemo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="lbDemo_SelectedIndexChanged">
            <asp:ListItem Text="Ashwin"></asp:ListItem>
            <asp:ListItem Text="Bhasker"></asp:ListItem>
            <asp:ListItem Text="Chandru"></asp:ListItem>
            <asp:ListItem Text="Das"></asp:ListItem>
            <asp:ListItem Text="Das"></asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:ListView ID="lvDemo" runat="server">
            <LayoutTemplate>
                Name
                <br />
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
            <ItemSeparatorTemplate>
                <br />
            </ItemSeparatorTemplate>
        </asp:ListView>
    </div>
    </form>

C#:

protected void lbDemo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            if (ViewState["Table"] == null)
            {
                dt.Rows.Add(lbDemo.SelectedItem.Text);
                ViewState["Table"] = dt;
                this.lvDemo.DataSource = dt;
                this.lvDemo.DataBind();
            }
            else
            {
                dt = (DataTable)ViewState["Table"];
                dt.Rows.Add(lbDemo.SelectedItem.Text);
                this.lvDemo.DataSource = dt;
                this.lvDemo.DataBind();
            }

        }

Image: