Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using three dropdowns, in first dropdown i bind data from sql. In second dropdown i also bind data from sql and the third dropdown is bind according to second dropdown so in second dropdown there is autopostback property true. I have one search button which fetch the result from database on click.

I want to keep all the Dropdownlist value same after my result show on the page. One thing i did is, at the time of button click i make the session and check it at page load when it is not null bind the dropdown value. Till this point it is working fine i am able to bind prevoius selected values.
This is in my master page and result are display in result page, which is using this masterpage.

aspx :
ASP.NET
<asp:DropDownList ID="dd_category" runat="server" AutoPostBack="True"              
onselectedindexchanged="dd_category_SelectedIndexChanged">
</asp:DropDownList>

<asp:DropDownList ID="dd_subcategory" runat="server" ></asp:DropDownList>

<asp:Button ID="but_go" runat="server" Text="Go" onclick="but_go_Click"/>


page load :
C#
if (!IsPostBack)
    {
        FillStates();
        FillCategory();
    }


if (Session["state"] != null)
      {
            dd_state.SelectedValue = (string) Session["state"];
      }


aspx.cs :

C#
private void FillStates()
{
    SqlCommand cmd = new SqlCommand("sps_bindcountrystate", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@flag", 2);
    cmd.Parameters.AddWithValue("@CountryID", 1);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        dd_state.DataSource = objDs.Tables[0];
        dd_state.DataTextField = "s_name";
        dd_state.DataValueField = "s_id";
        dd_state.DataBind();
        dd_state.Items.Insert(0, "Select State");
    }
}

protected void dd_category_SelectedIndexChanged(object sender, EventArgs e)
{
    int CategoryID = Convert.ToInt32(dd_category.SelectedValue);
    FillSubCategory(CategoryID);
}

private void FillCategory()
{
    SqlCommand cmd = new SqlCommand("sps_bindcategory", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@flag", 1);
    cmd.Parameters.AddWithValue("@CategoryID", 0);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        dd_category.DataSource = objDs.Tables[0];
        dd_category.DataTextField = "category_name";
        dd_category.DataValueField = "category_id";
        dd_category.DataBind();
        dd_category.Items.Insert(0, "Select Category");
        dd_subcategory.Items.Insert(0, "Select Sub-Category");
    }
}

private void FillSubCategory(int CategoryID)
{
    SqlCommand cmd = new SqlCommand("sps_bindcategory", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@flag", 2);
    cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        dd_subcategory.DataSource = objDs.Tables[0];
        dd_subcategory.DataTextField = "subcategory_name";
        dd_subcategory.DataValueField = "subcategory_id";
        dd_subcategory.DataBind();
        dd_subcategory.Items.Insert(0, "Select Sub-Category");
    }
}

protected void but_go_Click(object sender, EventArgs e)
{
    Session["state"] = dd_state.SelectedValue;
    Session["category"] = dd_category.SelectedValue;
    Session["subcategory"] = dd_subcategory.SelectedValue;
    Response.Redirect("Results.aspx");
}


Now problem is when i again choose some different value in first dropdown and try to choose different value in second dropdown, the second dropdown is having postback so it refreshes page and according to session state it changes my first dropdown value back to prevoius one.

Tell me what to do so that new selected value does'nt change due to page refresh.
Posted
Updated 6-Aug-18 1:58am
v2
Comments
ZurdoDev 27-Apr-14 21:42pm    
You need to allow ViewState to work. In page load check for IsPostBack. ViewState will automatically retain the values for you.
Raj Negi 28-Apr-14 4:15am    
i already done it...actually these dropdowns are in my masterpage and results are showing in results.aspx page which is using master page. So i am binding in the masterpage pageload as you say but it's not working.
[no name] 28-Apr-14 3:56am    
Set enableViewState mode to True for that dropdown
Raj Negi 28-Apr-14 4:16am    
it's already true for all the three dropdowns.

you don't need session. view state will keep the selected values. I think you lost selected values on postback due to binding again and again on each post back. check the IsPostBack and then bind your first and second droupdowns. 3rd one can be bind on selected index change of the 2nd droupdown.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)             
    {
      //Bind 1st dropdown
      //bind 2nd dropdown
    }
}

// in your 2nd droupdown selected index changed event  
void Ddl2_Index_Changed(Object sender, EventArgs e) 
{
   //bind 3rd droupdown based on 2nd droupdown selected item value

}
 
Share this answer
 
v2
Comments
Raj Negi 28-Apr-14 4:13am    
i already done it...actually these dropdowns are in my masterpage and results are showing in results.aspx page which is using master page. So i am binding in the masterpage pageload as you say but it's not working.
Set AutoPostback Property and Enable View State property true for that particluar dropdownlist
 
Share this answer
 
Comments
Raj Negi 28-Apr-14 4:30am    
yes it is true. i update the question, may be it will help you to understand the problem.
put this in onselected index changed

..
C#
if (dd_category.SelectedValue != "0")
        {
            Session["catid"] = dd_category.SelectedValue;
            
            ViewState["category"] = dd_category.SelectedValue;
        }



check the IsPostBack Property and then bind your 1st ddl and 2nd ddl. 3rd one can be bind on selected index change of the 2nd droupdown.
 
Share this answer
 
You just need to check whether EnableViewState is set to true for all dropdowns.

Also remove below code from Page_Load event:
if (Session["state"] != null)
{
     dd_state.SelectedValue = (string) Session["state"];
}


Now all the 3 dropdown's selected values will remain selected after page postback (unless you redirect to the same page again on button click).
 
Share this answer
 
Comments
Richard Deeming 6-Aug-18 10:11am    
FOUR YEARS too late.

Stick to answering recent questions.
Gautam Raithatha 7-Jan-22 9:31am    
I think I do not need suggestion on which question to reply. Please just focus on technical stuff. Thanks!
Richard Deeming 7-Jan-22 9:34am    
Is your internet connection horrendously slow, or do you really only access this site every four years? 🤣

Everything you said back in 2018 had already been adequately covered by the previous solutions, which were posted in 2014. Posting a new solution to restate what had been said four years previously adds nothing to the discussion, and looks like "rep point hunting", which is against the rules.
Gautam Raithatha 7-Jan-22 9:50am    
Can you just check the updated date for the question? Its the same date as my first reply. And also check all the answers, which is most relevant.

My internet connection is fine, you just need to wear specs!
Richard Deeming 7-Jan-22 9:56am    
The "updated date" on the question gets updated whenever a solution is posted.

It's possible that solution #6 was posted in 2018, which would have dragged this question back to the top of the list, and was then deleted as spam.

But that still doesn't change the fact that your solution is simply restating what the other solutions have already said. Both a comment on the question and solution #4 mentioned EnableViewState. And solution #3 mentioned not using the session.

And I still don't understand why it took you 3½ years to respond to my first comment! :)

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