添加更新面板后,下拉列表不会更新

时间:2013-08-01 02:30:27

标签: c# asp.net drop-down-menu updatepanel

我有多个下拉列表框,所有查询相同的列,同一个表和来自同一个数据库。例如,在memberID列下,我有以下ID。

  1. 12345
  2. 23456
  3. 34567
  4. 我有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;
    
    
    
                }
    

    更新

    现在,当选择默认值时,我可以允许我的其他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);
            }
    
        }
    

1 个答案:

答案 0 :(得分:1)

首先,我会在标记中将下拉列表的 AutoPostBack 属性设置为true:

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

更新:我们可以在视图状态中存储选定的值,并在重新填充下拉列表后检索:

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

我已经测试了代码,它对我有用。希望它有所帮助!