我在 EditItemTemplate 内的 gridview 中绑定了一个 ddl。值绑定,但索引增加了一步。我试着作弊一点,它有所帮助,但它揭示了第二个问题。
<asp:DropDownList ID="ddl_GetLists"
AppendDataBoundItems="true"
DataSourceID="sourceListData"
DataValueField="PK_dn_ID"
DataTextField="fld_dn_name"
SelectedIndex='<%#Eval("listID")-1 %>'
runat="server" />
我假设它被视为一个数组,从 0 而不是 1 开始。第二个问题是,当应用“-1”时,它似乎将索引解释为值而不是 ID。
PK
1 Not Assigned
2 Test List One
3 Test List Two
5 Test List Three
7 Test List Four
NULL NULL
如果记录的 PK 为 2,则读取为“测试列表一”,PK 3 读取为“测试列表二”。没问题。当 PK 达到 5 时,它会显示“测试列表四”,而 7 会将其踢回 1 或“未分配”。
<EditItemTemplate>
<div class="gridName">
<asp:TextBox ID="txtFirstName" Text='<%#Eval("firstName") %>' runat="server" Width="95" />
</div>
<div class="gridName">
<asp:TextBox ID="txtLastName" Text='<%#Eval("lastName") %>' runat="server" Width="95" />
</div>
<div class="gridEmail">
<asp:TextBox ID="txtEmail" Text='<%#Eval("email") %>' runat="server" Width="245" />
</div>
<div class="gridName">
<asp:DropDownList ID="ddl_GetLists"
AppendDataBoundItems="true"
DataSourceID="sourceListData"
DataValueField="PK_dn_ID"
DataTextField="fld_dn_name"
SelectedIndex='<%#Bind("listID")-1 %>'
runat="server" />
</div>
</EditItemTemplate>
Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
usersGrid.EditIndex = e.NewEditIndex
BindData()
End Sub
Protected Sub usersGrid_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim userID As Integer = DirectCast(usersGrid.DataKeys(e.RowIndex).Value, Integer)
usersGrid_Delete(userID)
BindData()
End Sub
Protected Sub BindData()
Dim conn As New SqlConnection(connectionString)
Dim ad As New SqlDataAdapter("MAINT_DIST_GET_USERS", conn)
Dim ds As New DataSet()
ad.Fill(ds)
usersGrid.DataSource = ds
usersGrid.DataBind()
Dim al As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
Dim dl As New DataSet()
al.Fill(dl)
listGrid.DataSource = dl
listGrid.DataBind()
End Sub