似乎是一个微不足道的问题,但无法确定解决方案。
我有一个数据表的下拉列表。我正在尝试在位置0插入一个新项目,但是当加载控件时,我没有看到新的列表项或任何错误。
Public Sub Page_Load(ByVal sender As Object, ByVal e As eventargs) Handles Me.Load
If Not Page.IsPostBack Then
loadRegistrantAbstracts()
End If
End Sub
Public Sub loadRegistrantAbstracts()
Dim obj As New Lookups
Dim dtAbstracts As DataTable
dtAbstracts = obj.getAbstracts()
If dtAbstracts.Rows.Count > 0 Then
With ddlAbstracts
.DataSource = dtAbstracts
.DataTextField = "DisplayName"
.DataValueField = "AbstractID"
.DataBind()
.Items.Insert(0, New ListItem("Select Abstract..", "0"))
End With
End If
End Sub
答案 0 :(得分:0)
由于DropDownList被绑定,它将呈现DataTable中的“stuff”。您需要在DataTable中将“Select Abstract ...”选项添加为DataRow。当您更改.DataSource的源时,它将反映在DropDownList中。
您很可能是从数据库加载此内容,因此您可能决定在查询中包含“选择...”选项:
SELECT 0 AS id, 'Select Abstract ...' AS abstract_name, 0 AS sort_order
UNION
SELECT dbo.abstracts.id, dbo.abstracts.name AS abstract_name, 1 as sort_order FROM dbo.abstracts
ORDER BY sort_order, abstract_name
答案 1 :(得分:0)
答案,部分归功于HardCode,如下:
Public Sub Page_Load(ByVal sender As Object, ByVal e As eventargs) Handles Me.Load
If Not Page.IsPostBack Then
loadRegistrantAbstracts()
End If
End Sub
Public Sub loadRegistrantAbstracts()
Dim obj As New Lookups
Dim dtAbstracts As DataTable
dtAbstracts = obj.getAbstracts()
Dim row As DataRow = dtAbstracts.NewRow
row("DisplayName") = "Select Abstract..."
row("AbstractID") = "0"
dtAbstracts.Rows.InsertAt(row, 0)
If dtAbstracts.Rows.Count > 0 Then
With ddlAbstracts
.DataSource = dtAbstracts
.DataTextField = "DisplayName"
.DataValueField = "AbstractID"
.DataBind()
End With
End If
End Sub