下拉列表添加更新面板后不更新(Dropdownlist does not update after

2019-10-30 07:40发布

我有多个下拉列表框中的所有查询同一列,同一表并从同一个数据库。 例如下MEMBERID列我有以下的ID。

  1. 12345
  2. 23456
  3. 34567

和我有3个不同的下拉列表框,如下所示

    <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>

我加入了下拉列表之间更新面板,以便让我的下拉列表于任何更新/选择刷新。

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

例如,当选择memberID1 12345,其他2下拉列表(memberID2和3)将不显示ID 12345反之亦然,当memberID1选择下拉列表的默认值,则ID重新出现在其他2下拉框。 我已经添加了所有的DDL的缺省值。

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

如果我没有弄错更新面板的财产,是不是能够使该功能的工作?

这里是要求绑定代码。

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

现在我能够让我的其他DDL来重新绑定,并选择默认值时,缓存中的其他下拉列表选择的值。 然而,当我选择其他ID,该ID被取出,它不会再结合其他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);
        }

    }

Answer 1:

首先,我会设置下拉菜单来真正在我的标记AutoPostBack属性:

<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>

更新:我们可以存储选定数值的视图状态和重新建像下面的dropdownlists后检索:

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);
        }
    }
}

我已经测试的代码,它是为我工作。 希望能帮助到你!



文章来源: Dropdownlist does not update after adding update panel