【问题标题】:Why does DropDownList in Gridview increment value?为什么Gridview中的DropDownList会增加值?
【发布时间】:2023-03-26 16:50:01
【问题描述】:

我在 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

【问题讨论】:

  • 有人需要更多信息吗?

标签: gridview drop-down-menu selectedindex edititemtemplate


【解决方案1】:

好的,菜鸟的错误,抱歉。需要使用 SelectedValue 而不是 SelectedIndex。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2012-07-10
    • 2019-09-28
    • 2016-10-30
    • 1970-01-01
    相关资源
    最近更新 更多