我有多个下拉列表框中的所有查询同一列,同一表并从同一个数据库。 例如下MEMBERID列我有以下的ID。
- 12345
- 23456
- 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);
}
}