Asp.net下拉列表问题

时间:2017-08-24 18:48:00

标签: c# asp.net dropdown

我有一个在单独的函数中设置的下拉列表。因此,首先加载页面时,它将从SQL数据库中获取一个值,并将该值传递给填充下拉列表的方法。 populate方法根据收到的参数将项目插入下拉列表。

此外,我在页面加载事件中有一段代码,如下所示

if (!IsPostBack) 
{
    // CALL THE POPULATE FUNCTION
}

上面的块只会在第一次加载页面时填充下拉列表。我不希望它通过按钮重新填充帖子背面

有一个搜索按钮,我必须输入一个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;
    }
}

有什么建议吗? 谢谢

1 个答案:

答案 0 :(得分:0)

您正在将""string.Empty)传递给ListItem构造函数:

DropDownList1.Items.Insert(0, new ListItem(t1, ""));

DropDownList必须设置有效值(不仅仅是浏览器列表中显示的文本)。这是通过DataValueField属性完成的。每个值必须是唯一的,否则您将最终处于只选择第一个(重复)的情况。

检查浏览器中的HTML源代码,您应该:

<select>
    <option value="some_unique_value1">text1</option>
    <option value="some_unique_value2">text2</option>
</select>

您需要这些唯一值,因为它们用于在服务器上选择正确的项目。

您是否尝试过更改构造函数以将其传递给某些唯一值?将""更改为Guid.NewGuid()或其他内容以进行测试。