2016-07-28 129 views
0

我已閱讀本網站和其他人的其他問題,但他們都沒有工作。我在我的GridView中有2列,如果點擊編輯鏈接,它應該會變成DropDownList。他們確實變成了DropDownList,但從未獲得任何數據。我確實把RowDataBound,但也沒有給我任何幫助。我試圖通過使用另一個函數顯式綁定它,但我一直沒有收到任何值。這裏是我的代碼:GridView EditItemTemplate中的DropDownList沒有填充數據

ASPX:

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

代碼背後:

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 

回答

0
Dim ddLFileType As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLFileType"), DropDownList) 
Dim ddLProduct As DropDownList = TryCast(dgvGameFIFolderMaintenance.FooterRow.FindControl("ddLProduct"), DropDownList) 

您正在尋找ddLProduct和頁腳ddLFileType當他們在編輯項模板聲明。

在你的RowDataBound中,你應該能夠切換到使用當前行。

Dim ddLFileType As DropDownList = TryCast(e.Row.FindControl("ddLFileType"), DropDownList) 
Dim ddLProduct As DropDownList = TryCast(e.Row.FindControl("ddLProduct"), DropDownList) 
+0

我試過了,沒有奏效。但我確實得到它的工作(GridView.FindControl(「...」) – MarchingGazelle