Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

using a dropdown to update another dropdown using postback

$
0
0

Hi all I have spent a long time looking for the answer to my problem. I am going through some code to use one database driven dropdown list to change the data listed in another dropdown list. The form looks up the country in the first list then applies the state you live in based on your country 

<asp:DropDownList ID="drplistcountry" runat="server"
                                    AutoPostBack="true" EnableViewState="true"
                                    OnSelectedIndexChanged="drplistcountry_SelectedIndexChanged"></asp:DropDownList>

the code for the page load and the next dropdown

string sqlserver = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    string user_id;
    protected void Page_Load(object sender, EventArgs e)
    {
       // fillresult();
        if (!Page.IsPostBack)
        {
            SqlDataReader drpSelectCountry;
            SqlConnection conmydata = new SqlConnection(sqlserver);
            conmydata.Open();
            SqlCommand cmd = new SqlCommand("select * from Country", conmydata);
            drpSelectCountry = cmd.ExecuteReader();
            drplistcountry.DataSource = drpSelectCountry;
            drplistcountry.DataTextField = "CountryName";
            drplistcountry.DataValueField = "CountryId";
            drplistcountry.DataBind();
            drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
            drpSelectCountry.Close();
            conmydata.Close();
            fillreligion();
        }
    }
     public void drplistcountry_SelectedIndexChanged(object sender, EventArgs e)
     {
         if (!Page.IsPostBack)
         {
             Response.Write("chaanged");
             SqlDataReader dtrSelectState;

             drpliststate.Visible = true;

             SqlConnection conmydata = new SqlConnection(sqlserver);
             conmydata.Open();
             SqlCommand cmd = new SqlCommand("select * from state where CountryId=@CountryID", conmydata);
             cmd.Parameters.Add("@CountryID", drplistcountry.SelectedValue.ToString());
             dtrSelectState = cmd.ExecuteReader();
             drpliststate.DataSource = dtrSelectState;
             drpliststate.DataTextField = "sname";
             drpliststate.DataValueField = "sid";
             drpliststate.DataBind();
             drpliststate.Items.Insert(0, new ListItem("--Select--", "0"));
             dtrSelectState.Close();
             conmydata.Close();
             //if (drpliststate.SelectedValue == "0")
             //{
             //    drplistcity.Items.Clear();
             //    drplistcity.Items.Insert(0, new ListItem("--Select--", "0"));
             //}
         }
     }

Tried a few things but what it looks like when you change the county name in the first  dd it does not postback 

Please help I am under the gun to get this project done

thank you


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>