Dropdownlist does not update after adding update p

2019-09-18 12:38发布

问题:

I have multiple dropdownlist box all querying the same column, same table and from the same database. For example under memberID column i have the following IDs.

  1. 12345
  2. 23456
  3. 34567

And i have 3 different dropdownlist box as shown below

    <asp:DropDownList ID="memberID3" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid3_SelectedIndexChanged" >
    </asp:DropDownList>
    <asp:DropDownList ID="memberID1" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid1_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="memberID2" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid2_SelectedIndexChanged">
    </asp:DropDownList>

I added update panel between the dropdownlist so as to allow my dropdownlist to refresh upon any updates/selections.

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
  </ContentTemplate>
  </asp:UpdatePanel>

For instance, when memberID1 selected 12345, the other 2 dropdownlist (memberID2 and 3) will not display the ID 12345. Vice versa, when memberID1 select the default value of the dropdownlist, the ID reappears in the other 2 dropdownlistbox. I have added a default value for all of the DDL.

 memeberID(number).Items.Insert(0, new ListItem(" Please select a Member ID", ""));
 memeberID(number).SelectedIndex = 0;

If i have not mistaken the property of update panel, isn't that able to make this function work?

here is the binding code as requested.

protected void ddllocation_SelectedIndexChanged(object sender, EventArgs e)
    {

        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);

                memberID1.Items.Clear();
                memberID1.DataSource = ds2;
                memberID1.DataTextField = "memberID";
                memberID1.DataValueField = "memberID";
                memberID1.DataBind();
                memberID1.Items.Insert(0, new ListItem("Please select a Member ID", ""));
                memberID1.SelectedIndex = 0;

                memberID2.Items.Clear();
                memberID2.DataSource = ds2;
                memberID2.DataTextField = "memberID";
                memberID2.DataValueField = "memberID";
                memberID2.DataBind();
                memberID2.Items.Insert(0, new ListItem("Please select a Member ID", ""));
                memberID2.SelectedIndex = 0;

                memberID3.Items.Clear();
                memberID3.DataSource = ds2;
                memberID3.DataTextField = "memberID";
                memberID3.DataValueField = "memberID";
                memberID3.DataBind();
                memberID3.Items.Insert(0, new ListItem("Please select a Member ID", ""));
                memberID3.SelectedIndex = 0;



            }

UPDATE

For now i'm able to allow my other ddl to re-bind and cache the selected value in the other dropdownlist when the default value is selected. However, when i select other ID, the ID are being removed and it wont re-bind the other ddl.

protected void FillddlLocations()
    {
        FillDropdown(ddlpid1);
        FillDropdown(ddlpid2);
        FillDropdown(ddlpid3);
        FillDropdown(ddlpid4);
        FillDropdown(ddlpid5);
        ddlpid1.Visible = false;
        ddlpid2.Visible = false;
        ddlpid3.Visible = false;
        ddlpid4.Visible = false;
        ddlpid5.Visible = false;
    }

    protected void FillDropdown(DropDownList ddl)
    {

        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);

                ddl.Items.Clear();
                ddl.DataSource = ds2;
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("Police ID", ""));
                ddl.SelectedIndex = 0;

            }

        }
    }

    protected void ddlpid1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlpid1.SelectedIndex > 0)
        {

            Session["pid1"] = ddlpid1.SelectedValue;

            ListItem removeItem2 = ddlpid2.Items.FindByValue(ddlpid1.SelectedValue);
            ddlpid2.Items.Remove(removeItem2);
            ListItem removeItem3 = ddlpid3.Items.FindByValue(ddlpid1.SelectedValue);
            ddlpid3.Items.Remove(removeItem3);
            ListItem removeItem4 = ddlpid4.Items.FindByValue(ddlpid1.SelectedValue);
            ddlpid4.Items.Remove(removeItem4);
            ListItem removeItem5 = ddlpid5.Items.FindByValue(ddlpid1.SelectedValue);
            ddlpid5.Items.Remove(removeItem5);


        }

        else

        if (ddlpid1.SelectedItem.Text.Equals("Police ID"))
        {
            FillDropdown(ddlpid1);
            FillDropdown(ddlpid2);
            FillDropdown(ddlpid3);
            FillDropdown(ddlpid4);
            FillDropdown(ddlpid5);


            ddlpid2.SelectedValue = (String)Session["pid2"];
            ddlpid2_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid3.SelectedValue = (String)Session["pid3"];
            ddlpid3_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid4.SelectedValue = (String)Session["pid4"];
            ddlpid4_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid5.SelectedValue = (String)Session["pid5"];
            ddlpid5_SelectedIndexChanged(this, EventArgs.Empty);
        }





    }

    protected void ddlpid2_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlpid2.SelectedIndex > 0)
        {

            Session["pid2"] = ddlpid2.SelectedValue;
            ListItem removeItem1 = ddlpid1.Items.FindByValue(ddlpid2.SelectedValue);
            ddlpid1.Items.Remove(removeItem1);
            ListItem removeItem3 = ddlpid3.Items.FindByValue(ddlpid2.SelectedValue);
            ddlpid3.Items.Remove(removeItem3);
            ListItem removeItem4 = ddlpid4.Items.FindByValue(ddlpid2.SelectedValue);
            ddlpid4.Items.Remove(removeItem4);
            ListItem removeItem5 = ddlpid5.Items.FindByValue(ddlpid2.SelectedValue);
            ddlpid5.Items.Remove(removeItem5);


        }
        else if (ddlpid2.SelectedItem.Text.Equals("Police ID"))
        {
            FillDropdown(ddlpid1);
            FillDropdown(ddlpid2);
            FillDropdown(ddlpid3);
            FillDropdown(ddlpid4);
            FillDropdown(ddlpid5);

            ddlpid1.SelectedValue = (String)Session["pid1"];
            ddlpid1_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid3.SelectedValue = (String)Session["pid3"];
            ddlpid3_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid4.SelectedValue = (String)Session["pid4"];
            ddlpid4_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid5.SelectedValue = (String)Session["pid5"];
            ddlpid5_SelectedIndexChanged(this, EventArgs.Empty);
        }


    }   

    protected void ddlpid3_SelectedIndexChanged(object sender, EventArgs e)
    {          
        if (ddlpid3.SelectedIndex > 0)
        {

            Session["pid3"] = ddlpid3.SelectedValue;
            ListItem removeItem1 = ddlpid1.Items.FindByValue(ddlpid3.SelectedValue);
            ddlpid1.Items.Remove(removeItem1);
            ListItem removeItem2 = ddlpid2.Items.FindByValue(ddlpid3.SelectedValue);
            ddlpid2.Items.Remove(removeItem2);
            ListItem removeItem4 = ddlpid4.Items.FindByValue(ddlpid3.SelectedValue);
            ddlpid4.Items.Remove(removeItem4);
            ListItem removeItem5 = ddlpid5.Items.FindByValue(ddlpid3.SelectedValue);
            ddlpid5.Items.Remove(removeItem5);


        }   
    else if (ddlpid3.SelectedItem.Text.Equals("Police ID"))
        {
            FillDropdown(ddlpid1);
            FillDropdown(ddlpid2);
            FillDropdown(ddlpid3);
            FillDropdown(ddlpid4);
            FillDropdown(ddlpid5);

            ddlpid1.SelectedValue = (String)Session["pid1"];
            ddlpid1_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid2.SelectedValue = (String)Session["pid2"];
            ddlpid2_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid4.SelectedValue = (String)Session["pid4"];
            ddlpid4_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid5.SelectedValue = (String)Session["pid5"];
            ddlpid5_SelectedIndexChanged(this, EventArgs.Empty);
        }


    }

    protected void ddlpid4_SelectedIndexChanged(object sender, EventArgs e)
    {            
        if (ddlpid4.SelectedIndex > 0)
        {
            Session["pid4"] = ddlpid4.SelectedValue;

            ListItem removeItem1 = ddlpid1.Items.FindByValue(ddlpid4.SelectedValue);
            ddlpid1.Items.Remove(removeItem1);
            ListItem removeItem2 = ddlpid2.Items.FindByValue(ddlpid4.SelectedValue);
            ddlpid2.Items.Remove(removeItem2);
            ListItem removeItem3 = ddlpid3.Items.FindByValue(ddlpid4.SelectedValue);
            ddlpid3.Items.Remove(removeItem3);
            ListItem removeItem5 = ddlpid5.Items.FindByValue(ddlpid4.SelectedValue);
            ddlpid5.Items.Remove(removeItem5);


        }

        else if (ddlpid4.SelectedItem.Text.Equals("Police ID"))
        {


            FillDropdown(ddlpid1);
            FillDropdown(ddlpid2);
            FillDropdown(ddlpid3);
            FillDropdown(ddlpid4);
            FillDropdown(ddlpid5);

            ddlpid1.SelectedValue = (String)Session["pid1"];
            ddlpid1_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid2.SelectedValue = (String)Session["pid2"];
            ddlpid2_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid3.SelectedValue = (String)Session["pid3"];
            ddlpid3_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid5.SelectedValue = (String)Session["pid5"];
            ddlpid5_SelectedIndexChanged(this, EventArgs.Empty);
        }


    }       

    protected void ddlpid5_SelectedIndexChanged(object sender, EventArgs e)
    {           

        if (ddlpid5.SelectedIndex > 0)
        {

            Session["pid5"] = ddlpid5.SelectedValue;
            ListItem removeItem1 = ddlpid1.Items.FindByValue(ddlpid5.SelectedValue);
            ddlpid1.Items.Remove(removeItem1);
            ListItem removeItem2 = ddlpid2.Items.FindByValue(ddlpid5.SelectedValue);
            ddlpid2.Items.Remove(removeItem2);
            ListItem removeItem3 = ddlpid3.Items.FindByValue(ddlpid5.SelectedValue);
            ddlpid3.Items.Remove(removeItem3);
            ListItem removeItem4 = ddlpid4.Items.FindByValue(ddlpid5.SelectedValue);
            ddlpid4.Items.Remove(removeItem4);

        }

    else if (ddlpid5.SelectedItem.Text.Equals("Police ID"))
        {
            FillDropdown(ddlpid1);
            FillDropdown(ddlpid2);
            FillDropdown(ddlpid3);
            FillDropdown(ddlpid4);
            FillDropdown(ddlpid5);

            ddlpid1.SelectedValue = (String)Session["pid1"];
            ddlpid1_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid2.SelectedValue = (String)Session["pid2"];
            ddlpid2_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid3.SelectedValue = (String)Session["pid3"];
            ddlpid3_SelectedIndexChanged(this, EventArgs.Empty);

            ddlpid4.SelectedValue = (String)Session["pid4"];
            ddlpid4_SelectedIndexChanged(this, EventArgs.Empty);
        }

    }

回答1:

First of all, I would set the AutoPostBack property for dropdowns to true in my markup:

<asp:DropDownList ID="memberID1" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid1_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="memberID2" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid2_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="memberID3" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid3_SelectedIndexChanged" >
    </asp:DropDownList>

Update: We can store selected values in view state and retrieve after repopulating the dropdownlists like below:

public partial class WebForm2 : System.Web.UI.Page
{
    bool bFlag = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillddlLocations();
        }
    }

    //Properties to store selected value in ViewState

    protected string MemberID1Selection
    {
        get
        {
            if (ViewState["MemberID1Selection"] != null)
                return ViewState["MemberID1Selection"].ToString();
            return "";
        }
        set { ViewState["MemberID1Selection"] = value; }
    }

    protected string MemberID2Selection
    {
        get
        {
            if (ViewState["MemberID2Selection"] != null)
                return ViewState["MemberID2Selection"].ToString();
            return "";
        }
        set { ViewState["MemberID2Selection"] = value; }
    }

    protected string MemberID3Selection
    {
        get
        {
            if (ViewState["MemberID3Selection"] != null)
                return ViewState["MemberID3Selection"].ToString();
            return "";
        }
        set { ViewState["MemberID3Selection"] = value; }
    }


    protected void FillddlLocations()
    {
        FillDropdown(memberID1);
        FillDropdown(memberID2);
        FillDropdown(memberID3);
        memberID1.Visible = true;
        memberID2.Visible = true;
        memberID3.Visible = true;
    }

    protected void FillDropdown(DropDownList ddl)
    {

        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);


                ddl.Items.Clear();
                ddl.DataSource = ds2;
                ddl.DataTextField = "memberID";
                ddl.DataValueField = "memberID";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("Please select a Member ID", ""));
                ddl.SelectedIndex = 0;

            }

        }
    }

    protected void IndexChanged(DropDownList ddlChanged, DropDownList ddlToFilter1, DropDownList ddlToFilter2)
    {
        string removeValue1 = ddlChanged == memberID1 ? MemberID1Selection : (ddlChanged == memberID2 ? MemberID2Selection : MemberID3Selection);
        string selValue2 = ddlChanged == memberID1 ? MemberID2Selection : (ddlChanged == memberID2 ? MemberID1Selection : MemberID1Selection);
        string selValue3 = ddlChanged == memberID1 ? MemberID3Selection : (ddlChanged == memberID2 ? MemberID3Selection : MemberID2Selection);

        bFlag = false;//Prevent fireing the code again while changing the index
        if (removeValue1 != "")
        {
            ListItem item1 = ddlToFilter1.Items.FindByValue(removeValue1);
            ddlToFilter1.Items.Remove(item1);
            ListItem item2 = ddlToFilter2.Items.FindByValue(removeValue1);
            ddlToFilter2.Items.Remove(item2);            
        }

        if (selValue3 != "")
        {
            ListItem item3 = ddlToFilter1.Items.FindByValue(selValue3);
            ddlToFilter1.Items.Remove(item3);
        }
        if (selValue2 != "")
        {
            ListItem item4 = ddlToFilter2.Items.FindByValue(selValue2);
            ddlToFilter2.Items.Remove(item4);
        }

        bFlag = false;
        ddlToFilter1.SelectedIndex = ddlToFilter1.Items.IndexOf(ddlToFilter1.Items.FindByValue(selValue2));
        ddlToFilter2.SelectedIndex = ddlToFilter2.Items.IndexOf(ddlToFilter2.Items.FindByValue(selValue3));
    }

    protected void ddlpid1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MemberID1Selection = memberID1.SelectedValue;
        if (bFlag)
        {
            FillDropdown(memberID2);
            FillDropdown(memberID3);
            IndexChanged(memberID1, memberID2, memberID3);
        }
    }

    protected void ddlpid2_SelectedIndexChanged(object sender, EventArgs e)
    {
        MemberID2Selection = memberID2.SelectedValue;
        if (bFlag)
        {
            FillDropdown(memberID1);
            FillDropdown(memberID3);
            IndexChanged(memberID2, memberID1, memberID3);
        }
    }
    protected void ddlpid3_SelectedIndexChanged(object sender, EventArgs e)
    {
        MemberID3Selection = memberID3.SelectedValue;
        if (bFlag)
        {
            FillDropdown(memberID1);
            FillDropdown(memberID2);
            IndexChanged(memberID3, memberID1, memberID2);
        }
    }
}

I have tested the code and it is working for me. Hope it helps!