locked
Call back function for asp:dropdown RRS feed

  • Question

  • User1132714760 posted

    Hi 

    I have 3 asp:drop down lists. DropDown2 values on DropDown1 selected Item.

    Dropdown3 values depend on dropdown2 selected item. I implemented the functionality using onchangeindex functionality. But it is doing pagerefresh on each dropdown item selected.

    I want to implement call back for this funtionalty to aviod page refresh.

    can some one share the code.

    Wednesday, January 30, 2019 11:47 PM

Answers

  • User839733648 posted

    Hi TejasviRebba,

    I also try the code in a Master page. I just delete the asp:ScriptManager, and it also worls well like below.

    If possible, please provide your detailed code and this will be easier to help with you.

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2019 9:02 AM

All replies

  • User839733648 posted

    Hi TejasviRebba,

    According to your description, I suggest that you could use Updatepanel Control to achieve your requirement.

    Updatepanel enables sections of a page to be partially rendered without a postback.

    I've made a sample and maybe you could refer to.

    .aspx

        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td>Country:</td>
                            <td>
                                <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Changed">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>State:</td>
                            <td>
                                <asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="true" OnSelectedIndexChanged="State_Changed">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>City:</td>
                            <td>
                                <asp:DropDownList ID="ddlCities" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </form>

    code-behind.

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string query = "select CountryId, CountryName from D_Countries";
                    BindDropDownList(ddlCountries, query, "CountryName", "CountryId", "Select Country");
                    ddlStates.Enabled = false;
                    ddlCities.Enabled = false;
                    ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
                    ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
                }
            }
    
            private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
            {
                string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlCommand cmd = new SqlCommand(query);
                using (SqlConnection con = new SqlConnection(conString))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        con.Open();
                        ddl.DataSource = cmd.ExecuteReader();
                        ddl.DataTextField = text;
                        ddl.DataValueField = value;
                        ddl.DataBind();
                        con.Close();
                    }
                }
                ddl.Items.Insert(0, new ListItem(defaultText, "0"));
            }
    
            protected void Country_Changed(object sender, EventArgs e)
            {
                ddlStates.Enabled = false;
                ddlCities.Enabled = false;
                ddlStates.Items.Clear();
                ddlCities.Items.Clear();
                ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
                ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
                int countryId = int.Parse(ddlCountries.SelectedItem.Value);
                if (countryId > 0)
                {
                    string query = string.Format("select StateId, StateName from D_States where CountryId = {0}", countryId);
                    BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State");
                    ddlStates.Enabled = true;
                }
            }
    
            protected void State_Changed(object sender, EventArgs e)
            {
                ddlCities.Enabled = false;
                ddlCities.Items.Clear();
                ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
                int stateId = int.Parse(ddlStates.SelectedItem.Value);
                if (stateId > 0)
                {
                    string query = string.Format("select CityId, CityName from D_Cities where StateId = {0}", stateId);
                    BindDropDownList(ddlCities, query, "CityName", "CityId", "Select City");
                    ddlCities.Enabled = true;
                }
            }

    result:

    Best Regards,

    Jenifer

    Thursday, January 31, 2019 3:46 AM
  • User1132714760 posted

    I cannot use asp:ScriptManager as I am integrating my page in another master page and it is already having asp:ScriptManager.

    So i cannot use on my page again. So planning  for call back function.

     

    Thursday, January 31, 2019 2:54 PM
  • User839733648 posted

    Hi TejasviRebba,

    I also try the code in a Master page. I just delete the asp:ScriptManager, and it also worls well like below.

    If possible, please provide your detailed code and this will be easier to help with you.

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2019 9:02 AM