asp.net VB asp:DropDownList只返回默认值

时间:2017-11-09 21:00:00

标签: asp.net vb.net

我的DropDownList仅返回默认选定选项。仅供参考,不使用代码。列表从数据库填充,但是当您选择任何选项时,它总是返回'NONE',这是第一个选项值。如果我从列表中删除所选的“NONE”项,则它根本不返回任何值。

代码:

<asp:DropDownList runat="server" ID="sublist" AppendDataBoundItems="true" EnableViewState="true">
    <asp:ListItem Selected="True" Text="None" Value="NONE" />
</asp:DropDownList>

填充列表:

Sub GenCatList()
    If Not IsPostBack Then
        SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
        RS2 = SQL2.ExecuteReader()
        sublist.DataSource = RS2
        sublist.DataTextField = "CategoryTitle"
        sublist.DataValueField = "CategoryTitle"
        sublist.DataBind()
        RS2.Close()
    End If
End Sub

表格提交:

Protected Sub AddCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim subcat As String = sublist.SelectedValue
    Response.Write(subcat)
    Response.End()
End Sub

3 个答案:

答案 0 :(得分:0)

我通常会将默认选项添加为绑定的一部分。正如您在标记中声明seleted='True'一样,情况也是如此。我做了以下事情:

<asp:DropDownList runat="server" ID="sublist" AppendDataBoundItems="true" EnableViewState="true">
</asp:DropDownList>

SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
Sub GenCatList()
    If Not IsPostBack Then
        SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
        RS2 = SQL2.ExecuteReader()
        sublist.DataSource = RS2
        sublist.DataTextField = "CategoryTitle"
        sublist.DataValueField = "CategoryTitle"
        sublist.DataBind()
        RS2.Close()

        //Add default option
        sublist.Items.Insert(new ListItem("None", "NONE").Selected = true)
    End If
End Sub

您的其他选择是尝试:

<!-- Note: the Selected attribute has been removed -->
<asp:DropDownList runat="server" ID="sublist" AppendDataBoundItems="true" EnableViewState="true">
    <asp:ListItem  Text="None" Value="NONE" />
</asp:DropDownList>

SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
Sub GenCatList()
    If Not IsPostBack Then
        SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
        RS2 = SQL2.ExecuteReader()
        sublist.DataSource = RS2
        sublist.DataTextField = "CategoryTitle"
        sublist.DataValueField = "CategoryTitle"
        sublist.DataBind()
        RS2.Close()

        //Set First item as selected
        sublist.Items(0).Selected = true
    End If
End Sub

注意我是一个C#家伙,所以你可能需要整理任一选项中的语法

答案 1 :(得分:0)

如果使用ExecuteReader(),则必须再次读取每条记录并添加到下拉列表中。例如:

<asp:DropDownList runat="server" ID="sublist" AppendDataBoundItems="true" EnableViewState="true">
</asp:DropDownList>

SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
Sub GenCatList()
    If Not IsPostBack Then
        SQL2.CommandText = "select * from Categories where SubCategory is NUll order by CategoryTitle ASC"
        RS2 = SQL2.ExecuteReader()

        While reader.Read()
        sublist.Items.Add(New ListItem(RS2.GetValue("CategoryTitle"), RS2.GetValue("CategoryTitle")))
        End While

        RS2.Close()

        //Add default option
        sublist.Items.Insert(new ListItem("None", "NONE").Selected = true)
    End If
End Sub

@ Jadeh评论后的更新。

您好@Jadeh,您是否都尝试过直接绑定方法?希望它。 你可以删除预插入列表项none(下面一个)并在绑定后将其插入零索引吗?作为&#34;无&#34;如果插入0,则将默认设置为

//Remove below pre-inserted line
<asp:ListItem Selected="True" Text="None" Value="NONE" />

//You may want to clear to make sure it's empty before bind.
sublist.Items.Clear()

//Bind data to sublist
con.Open()
sublist.DataSource = cmd.ExecuteReader()
sublist.DataTextField = "CategoryTitle"
sublist.DataValueField = "CategoryTitle"
sublist.DataBind()
con.Close()

//Insert at 0 index after bind.
sublist.Items.Insert(0, new ListItem("None", "NONE"))

答案 2 :(得分:0)

令人讨厌的是,我必须做的就是解决这个问题:

导入System.Configuration

抱怨自己的愚蠢