DropDownList in ASP.NET using C#

DropDownList in ASP.NET using C#

Hello everyone,

In this post, i'm going to share to code-sample for bind country list in dropdownlist in asp.net using C#.
 


This example is simple and easily understandable DropDownList code-sample using asp.net with c# 

Table of Contents

1. In the 1st steps, code-sample for aspx page
2. In the 2nd steps, code-sample for C# (.cs page)

In the 1st step, we write the code for view in asp.net page i.e. .aspx


<asp:DropDownList ID="DDLLocation" runat="server" OnSelectedIndexChanged="DDLLocation_SelectedIndexChanged"
    AutoPostBack="true">
    <asp:ListItem Text="Select Location" Selected="true"></asp:ListItem>
    <asp:ListItem Value="Nda">Utter Pradesh</asp:ListItem>
    <asp:ListItem Value="chi">Delhi</asp:ListItem>
    <asp:ListItem Value="Ambd">Ranchi</asp:ListItem>
    <asp:ListItem Value="us">USA</asp:ListItem>
    <asp:ListItem Value="kral">Kerela</asp:ListItem>
</asp:DropDownList>
              
In the 2nd step, we write the code for cs file in asp.net page i.e. .aspx.cs

/// <summary>
/// DropdownList Selected Index Changed evets.
/// </summary>
protected void DDLLocation_SelectedIndexChanged(object sender, EventArgs e)
{
    getRecordbyLocation();
}

/// <summary>
/// Get the Country details and bind the data in grid view.
/// </summary>
public void getRecordbyLocation()
{
    String connString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

    SqlDataAdapter da = new SqlDataAdapter("SP_getRecords", connString);

    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.Parameters.AddWithValue("@Location", DDLLocation.SelectedItem.Text);

    DataSet dsObj = new DataSet();

    da.Fill(dsObj);
    GridViewID.DataSource = dsObj.Tables[0];
    GridViewID.DataBind();

}

ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.