我有一个下拉列表被设置在一个单独的函数。所以,首先当页面加载它会从SQL数据库中获取一个值,并将该值传递给填充下拉列表的方法。根据收到的参数,填充方法将项目插入到下拉列表中。Asp.net下拉列表问题
另外我有代码在页面加载事件像下面
if (!IsPostBack)
{
// CALL THE POPULATE FUNCTION
}
上述块将仅填充下拉列表中第一次页面加载的块。我不希望它从一个按钮的邮政backs重新填充
有一个搜索按钮,我必须把一个ID和点击搜索。当我点击搜索时,下拉菜单会保留它的项目,但它会选择第一项。在我点击搜索之前,有时我会更改下拉选择,并且我希望它在点击搜索后保持这种状态,但它总是会返回到第一项。
基本上我有如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />
</div>
</form>
</body>
</html>
后面的代码:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Clear();
string test = "17/FO";
Populate(test);
}
}
protected void Populate(string t)
{
string t1, t2, t3, t4, t5, t6, t7, t8, t9, t10,t11,t12;
if (t.Contains("/FI"))
{
t1 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FI";
t2 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/SE";
t3 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/TH";
t4 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FO";
t5 = t.Substring(0, 2) + "/FI";
t6 = t.Substring(0, 2) + "/SE";
t7 = t.Substring(0, 2) + "/TH";
t8 = t.Substring(0, 2) + "/FO";
t9 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FI";
t10 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/SE";
t11 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/TH";
t12 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FO";
DropDownList1.Items.Insert(0, new ListItem(t1, ""));
DropDownList1.Items.Insert(1, new ListItem(t2, ""));
DropDownList1.Items.Insert(2, new ListItem(t3, ""));
DropDownList1.Items.Insert(3, new ListItem(t4, ""));
DropDownList1.Items.Insert(4, new ListItem(t5, ""));
DropDownList1.Items.Insert(5, new ListItem(t6, ""));
DropDownList1.Items.Insert(6, new ListItem(t7, ""));
DropDownList1.Items.Insert(7, new ListItem(t8, ""));
DropDownList1.Items.Insert(8, new ListItem(t9, ""));
DropDownList1.Items.Insert(9, new ListItem(t10, ""));
DropDownList1.Items.Insert(10, new ListItem(t11, ""));
DropDownList1.Items.Insert(11, new ListItem(t12, ""));
DropDownList1.SelectedIndex = 4;
}
else if (t.Contains("/SE"))
{
t1 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FI";
t2 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/SE";
t3 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/TH";
t4 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FO";
t5 = t.Substring(0, 2) + "/FI";
t6 = t.Substring(0, 2) + "/SE";
t7 = t.Substring(0, 2) + "/TH";
t8 = t.Substring(0, 2) + "/FO";
t9 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FI";
t10 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/SE";
t11 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/TH";
t12 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FO";
DropDownList1.Items.Insert(0, new ListItem(t1, ""));
DropDownList1.Items.Insert(1, new ListItem(t2, ""));
DropDownList1.Items.Insert(2, new ListItem(t3, ""));
DropDownList1.Items.Insert(3, new ListItem(t4, ""));
DropDownList1.Items.Insert(4, new ListItem(t5, ""));
DropDownList1.Items.Insert(5, new ListItem(t6, ""));
DropDownList1.Items.Insert(6, new ListItem(t7, ""));
DropDownList1.Items.Insert(7, new ListItem(t8, ""));
DropDownList1.Items.Insert(8, new ListItem(t9, ""));
DropDownList1.Items.Insert(9, new ListItem(t10, ""));
DropDownList1.Items.Insert(10, new ListItem(t11, ""));
DropDownList1.Items.Insert(11, new ListItem(t12, ""));
DropDownList1.SelectedIndex = 5;
}
else if (t.Contains("/TH"))
{
t1 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FI";
t2 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/SE";
t3 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/TH";
t4 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FO";
t5 = t.Substring(0, 2) + "/FI";
t6 = t.Substring(0, 2) + "/SE";
t7 = t.Substring(0, 2) + "/TH";
t8 = t.Substring(0, 2) + "/FO";
t9 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FI";
t10 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/SE";
t11 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/TH";
t12 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FO";
DropDownList1.Items.Insert(0, new ListItem(t1, ""));
DropDownList1.Items.Insert(1, new ListItem(t2, ""));
DropDownList1.Items.Insert(2, new ListItem(t3, ""));
DropDownList1.Items.Insert(3, new ListItem(t4, ""));
DropDownList1.Items.Insert(4, new ListItem(t5, ""));
DropDownList1.Items.Insert(5, new ListItem(t6, ""));
DropDownList1.Items.Insert(6, new ListItem(t7, ""));
DropDownList1.Items.Insert(7, new ListItem(t8, ""));
DropDownList1.Items.Insert(8, new ListItem(t9, ""));
DropDownList1.Items.Insert(9, new ListItem(t10, ""));
DropDownList1.Items.Insert(10, new ListItem(t11, ""));
DropDownList1.Items.Insert(11, new ListItem(t12, ""));
DropDownList1.SelectedIndex = 6;
}
else if (t.Contains("/FO"))
{
t1 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FI";
t2 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/SE";
t3 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/TH";
t4 = (int.Parse(t.Substring(0, 2)) - 1).ToString() + "/FO";
t5 = t.Substring(0, 2) + "/FI";
t6 = t.Substring(0, 2) + "/SE";
t7 = t.Substring(0, 2) + "/TH";
t8 = t.Substring(0, 2) + "/FO";
t9 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FI";
t10 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/SE";
t11 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/TH";
t12 = (int.Parse(t.Substring(0, 2)) + 1).ToString() + "/FO";
DropDownList1.Items.Insert(0, new ListItem(t1, ""));
DropDownList1.Items.Insert(1, new ListItem(t2, ""));
DropDownList1.Items.Insert(2, new ListItem(t3, ""));
DropDownList1.Items.Insert(3, new ListItem(t4, ""));
DropDownList1.Items.Insert(4, new ListItem(t5, ""));
DropDownList1.Items.Insert(5, new ListItem(t6, ""));
DropDownList1.Items.Insert(6, new ListItem(t7, ""));
DropDownList1.Items.Insert(7, new ListItem(t8, ""));
DropDownList1.Items.Insert(8, new ListItem(t9, ""));
DropDownList1.Items.Insert(9, new ListItem(t10, ""));
DropDownList1.Items.Insert(10, new ListItem(t11, ""));
DropDownList1.Items.Insert(11, new ListItem(t12, ""));
DropDownList1.SelectedIndex = 7;
}
}
有什么建议?
谢谢