2011-11-22 35 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。

相關問題