2011-11-22 81 views
0

我在一个EditItemTemplate中的GridView中绑定了一个ddl。值绑定,但索引正在增加一步。我尝试了一些作弊行为,但这有助于揭示第二个问题。为什么DropDownList在Gridview增量值中?

<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 

如果一个记录有2 PK,它读取“测试列表中的一个”,PK 3写着“测试列表二”。没问题。当PK命中5时,它显示“Test List Four”,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 
+0

任何人都需要更多信息? –

回答

0

好的,菜鸟的错误,对不起。需要使用SelectedValue而非SelectedIndex。