You must Sign In to post a response.
  • Category: XML

    To bind country, state, city

    how to bind country, state, city values from XML into Drop down list of the asp.net page with out page post back
  • #667397
    If you don't want to popstback then you can AJAXUpdate panel and use Selected index change event for binding XML values to dropdownlists.
    check following link it may help you

    http://www.csgnetwork.com/directcountrystatecitydrop.html
    http://www.c-sharpcorner.com/uploadfile/rohatash/dropdownlist-with-country-state-and-city-in-asp-net/

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #667401
    [Response removed by Admin. Read forum policies.]
    Regards,
    Prasad.g

    CountryStateCity.zip

    Delete Attachment

  • #667405
    Please check the following code:

    XmlFile.xml
    -----------


    <data>
    <Country id="1" name="India" />
    <Country id="2" name="Australia" />
    <Country id="3" name="USA" />
    <State id="1" name="Maharastra" cid="1" />
    <State id="2" name="West Bengal" cid="1" />
    <State id="3" name="Orissa" cid="1" />
    <State id="4" name="Melbourne" cid="2" />
    <State id="5" name="New Jersy" cid="3" />
    <City id="1" name="Mumbai" sid="1" />
    <City id="2" name="Pune" sid="1" />
    <City id="3" name="Kolkata" sid="2" />
    <City id="4" name="Kharagpur" sid="2" />
    <City id="5" name="Bhubaneswar" sid="3" />
    <City id="6" name="Cuttack" sid="3" />
    <City id="7" name="AusCity1" sid="4" />
    <City id="8" name="USACity2" sid="5" />
    </data>


    Default.aspx
    ------------


    <div>
    <asp:DropDownList ID="DdlCountry" runat="server" AutoPostBack="True"
    onselectedindexchanged="DdlCountry_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="DdlState" runat="server" AutoPostBack="True"
    onselectedindexchanged="DdlState_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="DdlCity" runat="server">
    </asp:DropDownList>
    </div>


    Default.aspx.cs
    ---------------


    public static DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    ds = new DataSet();
    ds.ReadXml(Server.MapPath("~/XMLFile.xml"));
    BindCountry();
    }
    }

    public void BindCountry()
    {
    DdlCountry.DataTextField = "name";
    DdlCountry.DataValueField = "id";
    DdlCountry.DataSource = ds.Tables["Country"];
    DdlCountry.DataBind();
    BindState(DdlCountry.SelectedValue);
    }

    public void BindState(string cid)
    {
    DdlState.DataTextField = "name";
    DdlState.DataValueField = "id";
    DataView dv = new DataView(ds.Tables["State"]);
    dv.RowFilter = "cid = " + cid;
    DdlState.DataSource = dv.ToTable();
    DdlState.DataBind();
    BindCity(DdlState.SelectedValue);
    }

    public void BindCity(string sid)
    {
    DdlCity.DataTextField = "name";
    DdlCity.DataValueField = "id";
    DataView dv = new DataView(ds.Tables["City"]);
    dv.RowFilter = "sid = " + sid;
    DdlCity.DataSource = dv.ToTable();
    DdlCity.DataBind();

    }

    protected void DdlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
    BindState(DdlCountry.SelectedValue);
    }

    protected void DdlState_SelectedIndexChanged(object sender, EventArgs e)
    {
    BindCity(DdlState.SelectedValue);
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #667464
    Hi Paritosh,

    I tried with different way and that is works fine. but it is very simple. Thank for your reply Paritosh.

  • #667512
    Hi Anitha,
    Could you send code which you tried in different way and which is very simple.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.