简体   繁体   中英

DropDownList selected index changed after postback in asp

I Have 3 DropDownList on my webpage first for State, Second for Districts and third for Cities.

<asp:DropDownList ID="stateddl" OnSelectedIndexChanged="stateddl_SelectedIndexChanged" AutoPostBack="true" runat="server" CssClass="form-control"></asp:DropDownList>
                Select District:
               <asp:DropDownList ID="districtddl" OnSelectedIndexChanged="districtddl_SelectedIndexChanged" AutoPostBack="true" runat="server" CssClass="form-control" ViewStateMode="Enabled"></asp:DropDownList>
                Select Area:
               <asp:DropDownList ID="cityddl" runat="server" CssClass="form-control"></asp:DropDownList>

code behind

protected void districtddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con2 = new SqlConnection("my connection");
            SqlCommand com = new SqlCommand("select DISTINCT Districtname,location from indiapincodes where statename =@statename and Districtname=@districtname order by Districtname", con2);
            SqlDataAdapter adpt = new SqlDataAdapter(com);
            com.Parameters.AddWithValue("@statename", stateddl.SelectedValue.ToString());
            com.Parameters.AddWithValue("@districtname", districtddl.SelectedValue.ToString());
            DataTable dt2 = new DataTable();
            adpt.Fill(dt2);
            cityddl.DataSource = dt2;
            cityddl.DataBind();
            cityddl.DataTextField = "location";
            cityddl.DataBind();
            con2.Close();
            con2.Dispose();
        }
        catch (Exception ex)
        {
        }
    }

I've populating states from the db on page load, if user select any state then load all districts of selected states on selectedindexchanged event. But the problem is, if user select any state selectedindexchanged event fire and postback. After postback DropDownList select first value of ddl every time. How can i solve this problem. Sorry for my bad English.

Without viewing your Page_Load event, most likely you are running the initial population of the dropdown on every postback. What you need to do is check for the initial load in the page load event using the Page.IsPostBack which tells you if the page is accessing the server for the first time or not.

private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        YourPopulationOfDdFunction();
    }
}
    Narender Godara! i think the issue is when u call first drop down on page load u have to use condition
    if(!ispostback)
    {
    //code of first dropdown %states
    } 
    then u have to write the next code in the selectedindexchanged of first dropdown and so on....
by the way your english is better than many developers of aisa, so bro keep it up, stay blessed,
#Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM