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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.