【问题标题】:Dropdownlist in Gridview not populating during Edit Mode在编辑模式期间未填充 Gridview 中的下拉列表
【发布时间】:2013-01-30 14:28:46
【问题描述】:

我有一个asp gridview,我想要在所选行切换到编辑模式时填充dropdownlist控件。

<EditItemTemplate>
      <asp:DropDownList ID="ddlFormatID" runat="server">
      </asp:DropDownList>

我已经四处搜索,我知道您应该在 rowdatabound 上执行此操作,然后检查该行是否是正在编辑的行,如果是,则填充 DDL,但我无法检查该行正常工作:(

        If DataControlRowState.Edit = e.Row.RowState Then

        Dim ddlFormat As DropDownList = e.Row.FindControl("ddlFormatID")
        ddlFormat.DataSource = XRefBCWorker.GetFormatCombos
        ddlFormat.DataTextField = "Format"
        ddlFormat.DataValueField = "FormatID"
        ddlFormat.DataBind()

    End If

我做错了什么?

【问题讨论】:

标签: asp.net vb.net gridview


【解决方案1】:

我能够找到这篇文章,并且我在 rowdatabound 事件中更改了我的代码并且它起作用了!

        If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.DataItem IsNot Nothing Then
            If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
                Dim ddlFormat As DropDownList = e.Row.FindControl("ddlFormatID")
                ddlFormat.DataSource = XRefBCWorker.GetFormatCombos
                ddlFormat.DataTextField = "Format"
                ddlFormat.DataValueField = "FormatID"
                ddlFormat.DataBind()
                ddlFormat.SelectedIndex = CurrentFormatID
            End If
        End If
    End If

【讨论】:

  • 我很高兴你知道了。我想知道您是如何获得 CurrentFormatID 的?
【解决方案2】:

您可以将 DataItem 转换为您显示的类型。

Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If DataControlRowState.Edit = e.Row.RowState Then
        Dim item = e.Row.DataItem
        Dim dr = DirectCast(item, DataRowView)
        Dim id = Integer.Parse(dr(0).ToString())
    End If
End Sub

【讨论】:

    【解决方案3】:

    只需将其添加到标记中:

    <asp:DropDownList SelectedValue='<%# Bind("categoryId") %>'
    

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多