Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code snipnets are given below.
XML
<asp:DropDownList ID="ddlState" runat="server" Height="28px" Width="354px" 
                            BorderColor="#C9CACA" BorderStyle="Solid" BorderWidth="1px" BackColor="White" 
                                change="UpdateDistrict()" >
                            
<script language="javascript" type="text/javascript">
    var xmlHttp;
    function UpdateDistrict()
    {
    xmlHttp = null;
    debugger;
    if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    }
    
    if(xmlHttp != null)
    {
        var contName = document.getElementById('<%=ddlState.ClientID %>').value; 
        
        xmlHttp.onreadystatechange=state_Change;
        
        xmlHttp.open("GET","frmForAjaxCalls.aspx?cont="+contName,true);
        xmlHttp.send(null); 
    }
    }
</script>

C#
public partial class frmForAjaxCalls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Statename = Request.QueryString["cont"] as string;

        if (Statename != null)
        {
            DataTable table = Class1.getDistrictname(Statename);

            string result = string.Empty;

            foreach (DataRow r in table.Rows)
            {
                result += r["DistrictName"].ToString() + ";";
            }

            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
}

this is within the class
public static DataTable getDistrictname(String StateSelected)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        con.Open();
        SqlDataAdapter da2 = new SqlDataAdapter("select '--Not in List--' as DistrictName from tblMSTStateDistrict UNION select distinct DistrictName from tblMSTStateDistrict where Stateid ='" + Convert.ToString(StateSelected) + "' order by DistrictName", con);
        DataSet ds2 = new DataSet();
        da2.Fill(ds2);

        return ds2.Tables[0];
        con.Close();

    }

this is within the code file
onselectedindexchanged="ddlState_SelectedIndexChanged" onchange="UpdateDistrict()"

if (!PostBack)
{
SqlDataAdapter da1 = new SqlDataAdapter("select 0 as Stateid, '' as StateName from tblMSTStateMaster UNION select distinct Stateid, StateName from tblMSTStateMaster", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
ddlState.DataSource = ds1.Tables[0];
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataBind();

ddlDistrict.DataSource = Class1.getDistrictname(ddlState.SelectedValue);
        ddlDistrict.DataTextField = "DistrictName";
        ddlDistrict.DataValueField = "DistrictName";
        ddlDistrict.DataBind();
}

The problem is that the state is populating. The control is also fatching "--No in list--" when !not postback. But when I am selecting the state the district ddl is not getting populated. Moreover it is not executing the javascript updateDistrict()
Kindly help me out.
Posted
v2

1 solution

use below link you will get an example
postback[^]

Note:-Autopostback set to true of state.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900