为什么Gridview中的DropDownList会增加值?

时间:2011-11-22 21:13:31

标签: gridview drop-down-menu selectedindex edititemtemplate

我在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,则为“Test List One”,PK 3为“Test List Two”。没问题。当PK命中5时,它会显示“Test List Four”,7会将其踢回1或“Not Assigned”。

<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

1 个答案:

答案 0 :(得分:0)

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