Home > Software design >  How to refresh dropdown list after setting index to 0?
How to refresh dropdown list after setting index to 0?

Time:07-13

I already have an event handler for a button that generates an excel report. (ExportExcel(filename); is the method that generates the excel report FYI) So that's fine and dandy. What I'm trying to do now is refresh the dropdown list so it reflects what the correct Dropdown selection is.

I got it to set the dropdown list to the 0th index but the dropdown list doesn't change for some reason. So I think I need to somehow individually reset the dropdown list. Here's some code below. (I left in some comments and Debug.WriteLine()'s to show my thought process)

protected void GenerateButton1(object sender, EventArgs e)
        {
            // CustomerDropdown.ClearSelection();
            // CustomerDropdown.Items.Clear();
            // Page.Response.Redirect(Page.Request.Url.ToString(), true);
            //////////
            // Below is resetting the CustomerDropdown list.
            ViewState["DropDownIndexSelection"] = CustomerDropdown.SelectedItem.Value.Trim();
            DataSet dataSet = (DataSet)ViewState["DropDownListDs"];
            var filename = "Customer ID - "   DateTime.Now   ".xlsx";
            ExportExcel(filename);
            // DataSet DropDownIndexSelection = (DataSet)ViewState["DropDownIndexSelection"];
            CustomerDropdown.Items.Clear();
            // CustomerDropdown.ClearSelection();
            CustomerDropdown.DataTextField = dataSet.Tables[0].Columns["cust_id"].ToString();
            CustomerDropdown.DataValueField = dataSet.Tables[0].Columns["cust_id"].ToString();
            CustomerDropdown.DataSource = dataSet.Tables[0];  //assigning datasource to the dropdownlist  
                                                              // CustomerDropdown.Items[0].Selected = true;
            CustomerDropdown.DataBind();  //binding dropdownlist 
            CustomerDropdown.Items.Insert(0, new ListItem("Please select"));
            CustomerDropdown.Items.Insert(1, new ListItem("All"));

            CustomerDropdown.Items.FindByValue("Please select").Selected = true;
            Debug.WriteLine(CustomerDropdown.Items.FindByValue("Please select").Selected == false);
            Debug.WriteLine(CustomerDropdown.Items.FindByValue("Please select").Selected == true);
return;
}

How can I set the Dropdown back to "Please select" after running the ExportExcel(filename) method? I think basically what I'm asking is how do I refresh the CustomerDropdown after setting the selection to "Please select"

CodePudding user response:

CustomerDropdown.SelectedIndex = 0;

CodePudding user response:

after that it will select the 0th index selection, which is "Please select."

Have you tried adding to your code:

        ExportExcel(filename);
        // DataSet DropDownIndexSelection = (DataSet)ViewState["DropDownIndexSelection"];
        CustomerDropdown.Items.Clear();
        // CustomerDropdown.ClearSelection();
        CustomerDropdown.DataTextField = dataSet.Tables[0].Columns["cust_id"].ToString();
        CustomerDropdown.DataValueField = dataSet.Tables[0].Columns["cust_id"].ToString();
        CustomerDropdown.DataSource = dataSet.Tables[0];  //assigning datasource to the dropdownlist  
                                                          // CustomerDropdown.Items[0].Selected = true;
        CustomerDropdown.DataBind();  //binding dropdownlist 
        CustomerDropdown.Items.Insert(0, new ListItem("Please select"));
        CustomerDropdown.Items.Insert(1, new ListItem("All"));
        CustomerDropdown.SelectedIndex=0; ---> Possible solution

?

Got from: Reference

  • Related