How to bind dropdown list within listview in ASP.NET

No.of Views5721
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  10 Oct 2010 10:10:42
Tag : ASP.NET , List Controls
In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet, I would like to show how to bind the dropdown list within listview. Because I have get some questions in codegain forums most of the readers asking how we could bind or dynamically add item into dropdownlist with ListView.
So first I created a aspx page like followings,

<asp:ListView runat="server" id="lvPopUp">
        <InsertItemTemplate>
            <tr runat="server">
                <td>
                    <asp:DropDownList ID="cmbYears" runat="server">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>' Width="100px">First Name</asp:TextBox>
                    <asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>' Width="100px">Last Name</asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="txtCtype" runat="server" Text='<%#Eval("ContactType") %>' Width="100px">Contact Type</asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                </td>
            </tr>
        </InsertItemTemplate>
    </asp:ListView>

Now i have to load list of years in within dropdownlsit in InsertItemTemplate. so this point we have to choice right event to bind Years list within the control.if you re choice ItemDataBound events it won't bind. because this is raise after controls are created and databinding times.

So we have to choice the ItemCreated event to bind data to year dropdown list.further reading about ItemCreated here

Code

protected void lvPopUp_ItemCreated(object sender, ListViewItemEventArgs e)
    {if (e.Item.ItemType == ListViewItemType.InsertItem)
        {
            DropDownList ddl = (DropDownList)e.Item.FindControl("cmbYears");if (ddl != null)
            {
                ddl.DataSource = LoadYear();
                ddl.DataTextField = "Year";
                ddl.DataValueField = "Value";
                ddl.DataBind();

            }
        }
    }

In above event code, i'm checking ItemType before bound data to control, because we have to bind list of years which is the ItemInsertTemplate Dropdown list.I hope this is help to you all. thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:AuthorDate Of Posted:4/20/2011 11:27:44 PM
Thank you
Hi Paul, Thank you for your nice comments, we are expect your support to codegain to grow more and more. thank you
By:Paul GDate Of Posted:3/30/2011 9:56:07 AM
Fantastic
This works a treat. I've been trying to reference the nested dropdownlist for hours and was about to give in. Thank you very much
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top