首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未使用数据填充GridView EditItemTemplate中的DropDownList

未使用数据填充GridView EditItemTemplate中的DropDownList
EN

Stack Overflow用户
提问于 2016-07-28 22:53:16
回答 1查看 148关注 0票数 0

我在这个网站和其他网站上读过其他问题,但没有一个有效。我在我的GridView中有2列,它们应该在单击编辑链接时成为DropDownList。它们确实会变成DropDownList,但永远不会获得任何数据。我确实放了一个RowDataBound,但这也不能给我任何帮助。我尝试使用另一个函数来显式绑定它,但一直没有返回任何值。下面是我的代码:

ASPX:

代码语言:javascript
运行
复制
<asp:GridView ID="dgvGameFIFolderMaintenance" runat="server" AutoGenerateColumns="false" 
CssClass="ReportDataGrid" HeaderStyle-CssClass="DataGridHeader" FooterStyle-CssClass="NoShade" 
RowStyle-CssClass="AccentShade" AlternatingRowStyle-CssClass="NoShade" SelectedRowStyle-CssClass="AccentLvl3" 
PagerSettings-Mode="NumericFirstLast" PagerStyle-HorizontalAlign="Center" PagerStyle-CssClass="paging" 
PageSize="25" AllowSorting="false" ShowFooter="true" ShowHeaderWhenEmpty="true" AllowPaging="true" 
Width="900px" OnRowCancelingEdit="CancelEditRecord" OnRowEditing="EditRecord" OnRowUpdating="UpdateRecord"> 
    <Columns>
    <asp:TemplateField HeaderText="Account ID" ItemStyle-Width="110px">
    <ItemTemplate>
    <asp:Label ID="lblAccountID" runat="server" Text='<%#Eval("Account_ID")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtAccountID" runat="server" Text='<%#Eval("Account_ID")%>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtAccountIDFT" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="File Type" ItemStyle-Width="110px">
    <ItemTemplate>
    <asp:Label ID="lblFileType" runat="server" Text='<%#Eval("FileType")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Label ID="lblFileType" runat="server" Text='<%#Eval("FileType")%>' Visible="false"></asp:Label>
    <asp:DropDownList ID="ddlFileType" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:DropDownList ID="ddlFileTypeFT" runat="server" Width="98%"></asp:DropDownList>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Drop Location" ItemStyle-Width="110px">
    <ItemTemplate>
    <asp:Label ID="lblDropLocation" runat="server" Text='<%#Eval("DropLocation")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtDropLocation" runat="server" Text='<%#Eval("DropLocation")%>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtDropLocationFT" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product" ItemStyle-Width="110px">
    <ItemTemplate>
    <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product")%>' Visible="false"></asp:Label>
    <asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:DropDownList ID="ddlProductFT" runat="server" Width="98%"></asp:DropDownList>
    </FooterTemplate>
     <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"
    OnClientClick="retrun confirm('Are you sure you want to delete?');"></asp:LinkButton>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button ID="btnAdd" runat="server" CommandName="AddNew" Text="Add" OnClick="AddNewRecord" />
    </FooterTemplate>
    </asp:TemplateField>
    <asp:CommandField ButtonType="Link" ShowEditButton="true" HeaderText="Edit" EditText="Edit" />
    </Columns>
    </asp:GridView>

代码隐藏:

代码语言:javascript
运行
复制
 Protected Sub BindData()
    BindGameFIData()
    BindFileTypeDDLFT()
    BindProductDDLFT()
End Sub
Protected Sub BindGameFIData()
    Dim dsGameFIData As New DataSet
    Dim lDB As New RHubCoreFunctions.RHubDB
    dsGameFIData = lDB.GetDataSet("SSP_GameFI_FileDropLocation")
    dgvGameFIFolderMaintenance.DataSource = dsGameFIData
    dgvGameFIFolderMaintenance.DataBind()
End Sub
Protected Sub BindFileTypeDDLFT()
    Dim dsFileTypeDDL As New DataSet
    Dim lDB As New RHubCoreFunctions.RHubDB
    dsFileTypeDDL = lDB.GetDataSet("SSP_GameFI_FTDDL")
    Dim ddLFileType As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLFileTypeFT"), DropDownList)
    ddLFileType.DataSource = dsFileTypeDDL.Tables(0)
    ddLFileType.DataValueField = "FileType"
    ddLFileType.DataTextField = "FileType"
    ddLFileType.DataBind()
    ddLFileType.Items.Insert(0, New ListItem("-Select-", "0"))
End Sub
Protected Sub BindFileTypeDDL()
    Dim dsFileTypeDDL As New DataSet
    Dim lDB As New RHubCoreFunctions.RHubDB
    dsFileTypeDDL = lDB.GetDataSet("SSP_GameFI_FTDDL")
    Dim ddLFileType As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLFileType"), DropDownList)
    ddLFileType.DataSource = dsFileTypeDDL.Tables(0)
    ddLFileType.DataValueField = "FileType"
    ddLFileType.DataTextField = "FileType"
    ddLFileType.DataBind()
    'ddLFileType.Items.Insert(0, New ListItem("-Select-", "0"))
End Sub
Protected Sub BindProductDDLFT()
    Dim dsProductDDL As New DataSet
    Dim lDB As New RHubCoreFunctions.RHubDB
    dsProductDDL = lDB.GetDataSet("SSP_GameFI_DDL")
    Dim ddLProduct As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLProductFT"), DropDownList)
    ddLProduct.DataSource = dsProductDDL.Tables(0)
    ddLProduct.DataValueField = "Product"
    ddLProduct.DataTextField = "Product"
    ddLProduct.DataBind()
    ddLProduct.Items.Insert(0, New ListItem("-Select-", "0"))
End Sub
Protected Sub BindProductDDL()
    Dim dsProductDDL As New DataSet
    Dim lDB As New RHubCoreFunctions.RHubDB
    dsProductDDL = lDB.GetDataSet("SSP_GameFI_DDL")
    Dim ddLProduct As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLProduct"), DropDownList)
    ddLProduct.DataSource = dsProductDDL.Tables(0)
    ddLProduct.DataValueField = "Product"
    ddLProduct.DataTextField = "Product"
    ddLProduct.DataBind()
    'ddLProduct.Items.Insert(0, New ListItem("-Select-", "0"))
End Sub


Protected Sub EditRecord(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    dgvGameFIFolderMaintenance.EditIndex = e.NewEditIndex
    BindData()
    BindFileTypeDDL() //Added because RowDatabound was not working
    BindProductDDL() //Added because RowDatabound was not working
End Sub

Protected Sub UpdateRecord(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim iD As Integer = Convert.ToInt32(dgvGameFIFolderMaintenance.DataKeys(e.RowIndex).Values("ID")).ToString()
    Dim accountID As String = DirectCast(dgvGameFIFolderMaintenance.Rows(e.RowIndex).FindControl("txtAccountID"), TextBox).Text
    Dim fileType As String = TryCast(dgvGameFIFolderMaintenance.Rows(e.RowIndex).FindControl("ddlFileType"), DropDownList).SelectedItem.Value
    Dim dropLocation As String = DirectCast(dgvGameFIFolderMaintenance.Rows(e.RowIndex).FindControl("txtDropLocation"), TextBox).Text
    Dim product As String = DirectCast(dgvGameFIFolderMaintenance.Rows(e.RowIndex).FindControl("ddlProduct"), DropDownList).SelectedItem.Value
    Dim lDB As New RHubCoreFunctions.RHubDB
    Dim updateStatus As Integer
    Dim asParams(4) As IDbDataParameter
    asParams(0) = lDB.dpCreateDataParameter("@ID", DbType.Int32, 20, ID, ParameterDirection.Input)
    asParams(1) = lDB.dpCreateDataParameter("@Account_id", DbType.String, 20, accountID, ParameterDirection.Input)
    asParams(2) = lDB.dpCreateDataParameter("@FileType", DbType.String, 20, fileType, ParameterDirection.Input)
    asParams(3) = lDB.dpCreateDataParameter("@DropLocation", DbType.String, 100, dropLocation, ParameterDirection.Input)
    asParams(4) = lDB.dpCreateDataParameter("@Product", DbType.String, 100, product, ParameterDirection.Input)
    updateStatus = lDB.GetNonQuery("usp_GameFI_FileDropLocation", asParams)
    'lblPopup.Text = "Record Updated Successfully"
    'pnlPopup_ModalPopupExtender.Show()
    dgvGameFIFolderMaintenance.EditIndex = -1
    BindData()
End Sub
Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow AndAlso dgvGameFIFolderMaintenance.EditIndex = e.Row.RowIndex Then
        Dim dsProductDDL As New DataSet
        Dim lDB As New RHubCoreFunctions.RHubDB
        dsProductDDL = lDB.GetDataSet("SSP_GameFI_DDL")
        Dim ddLProduct As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLProduct"), DropDownList)
        ddLProduct.DataSource = dsProductDDL.Tables(0)
        ddLProduct.DataValueField = "Product"
        ddLProduct.DataTextField = "Product"
        ddLProduct.DataBind()
        ddLProduct.Items.FindByValue(TryCast(e.Row.FindControl("lblProduct"), Label).Text).Selected = True
    End If
    If e.Row.RowType = DataControlRowType.DataRow AndAlso dgvGameFIFolderMaintenance.EditIndex = e.Row.RowIndex Then
        Dim dsFileTypeDDL As New DataSet
        Dim lDB As New RHubCoreFunctions.RHubDB
        dsFileTypeDDL = lDB.GetDataSet("SSP_GameFI_FTDDL")
        Dim ddLFileType As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLFileType"), DropDownList)
        ddLFileType.DataSource = dsFileTypeDDL.Tables(0)
        ddLFileType.DataValueField = "Product"
        ddLFileType.DataTextField = "Product"
        ddLFileType.DataBind()
        ddLFileType.Items.FindByValue(TryCast(e.Row.FindControl("lblFileType"), Label).Text).Selected = True
    End If
End Sub
EN

回答 1

Stack Overflow用户

发布于 2016-07-29 02:52:07

代码语言:javascript
运行
复制
Dim ddLFileType As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLFileType"), DropDownList)
Dim ddLProduct As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLProduct"), DropDownList)

当在编辑项模板中声明ddLProductddLFileType时,您将在页脚中查找它们。

在您的RowDataBound中,您应该能够切换到使用当前行。

代码语言:javascript
运行
复制
Dim ddLFileType As DropDownList = TryCast(e.Row.FindControl("ddLFileType"), DropDownList)
Dim ddLProduct As DropDownList = TryCast(e.Row.FindControl("ddLProduct"), DropDownList)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38639899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档