How to Bind Country, State, City using Ajax Update Panel



Many times I have seen a problem that many developers face binding a DropdownList using Ajax. There are many ways to do that but I just want to share a very simple way. .... Hope you people will like this!

Coding behind Default.aspx page

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="countrypanel" runat="server">
           <ContentTemplate >
              <asp:DropDownList ID="ddlcountry" AutoPostBack ="true" AppendDataBoundItems
="true"  runat="server" Height="20px" Width="156px"    
onselectedindexchanged="ddlcountry_SelectedIndexChanged">
            </asp:DropDownList>
        </ContentTemplate
>

      <Triggers>
       <asp:AsyncPostBackTrigger ControlID ="ddlcountry" />
      </Triggers>
   </asp:UpdatePanel>
        <br
/>

  <asp:UpdatePanel ID="statepanel" runat="server">     
     <ContentTemplate >
       <asp:DropDownList ID="ddlstate" AutoPostBack ="true"
        AppendDataBoundItems ="true"  runat="server" Height="20px"
Width="155px"                onselectedindexchanged="ddlstate_SelectedIndexChanged">
       </asp:DropDownList>
     </ContentTemplate>
     <Triggers >
        <asp:AsyncPostBackTrigger ControlID ="ddlstate" />
        </Triggers>
     </asp:UpdatePanel>
        <br
/>

<asp:UpdatePanel ID="citypanel" runat="server">      
    <ContentTemplate >       
      <asp:DropDownList ID="ddlcity"  AutoPostBack ="true"
    AppendDataBoundItems ="true" runat="server" Height="20px" Width="155px">
      </asp:DropDownList>
   </ContentTemplate>
   <Triggers >
     <asp:AsyncPostBackTrigger  ControlID ="ddlcity" />         </Triggers>
       
  </asp:UpdatePanel
>
 </div>
</
form>

C# Code:

Coding behind Default.aspx.cs page

public partial class _Default : System.Web.UI.Page
{

    SqlConnection conn = new SqlConnection("Data Source=YOGENDRA-PC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True");
    public void Bind_ddlCountry()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select CountryID,CountryName from tblCountry",conn );
        SqlDataReader dr = cmd.ExecuteReader();
        ddlcountry.DataSource = dr;
        ddlcountry.Items.Clear();
        ddlcountry.Items.Add("--Select--");
        ddlcountry.DataTextField = "CountryName";
        ddlcountry.DataValueField = "CountryID";
        ddlcountry.DataBind();
        conn.Close();

    }

    public void Bind_ddlState()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select StateID,StateName from tblState where Country_ID='"+ddlcountry .SelectedValue +"'",conn );
        SqlDataReader dr = cmd.ExecuteReader();
        ddlstate.DataSource = dr;
        ddlstate.Items.Clear();
        ddlstate.Items.Add("--Select--");
        ddlstate.DataTextField = "StateName";
        ddlstate.DataValueField = "StateID";
        ddlstate.DataBind();
        conn.Close();

    }

    public void Bind_ddlCity()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select CityID,CityName from tblCity where State_ID ='"+ddlstate .SelectedValue +"'",conn );
        SqlDataReader dr = cmd.ExecuteReader();
        ddlcity.DataSource = dr;
        ddlcity.Items.Clear();
        ddlcity.Items.Add("--Select--");
        ddlcity.DataTextField = "CityName";
        ddlcity.DataValueField = "CityID";
        ddlcity.DataBind();
        conn.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_ddlCountry();
        }
    }
    protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_ddlState();
    }
    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_ddlCity();
    }
}

 

Up Next
    Ebook Download
    View all
    Learn
    View all