【解決方法】グリッドビュー編集テンプレート


編集テンプレートを備えたグリッドビューがあります。 編集テンプレートには、グリッドビューの作成時に入力しようとしているドロップダウン リストが含まれています。 ただし、ドロップダウンリストを取得できません。 プロジェクト内の別の場所で同じ手順を実行したところ、正常に動作しましたが、この場合はドロップダウン リストが取得されませんでした。 よろしく:

私のグリッドコード:

XML
<asp:GridView ID="gvCallHistory" runat="server" AutoGenerateColumns="False"
                         CellPadding="3" Width="100%" Font-Names="Arial" Font-Size="14px"
                         DataKeyNames="LogID">
                         <RowStyle CssClass="ProfileRow" Wrap="False" />
                         <Columns>
                             <asp:TemplateField HeaderText="Time of Call">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtCallTime" runat="server" BorderStyle="None" Height="25px"
                                         Text='<%# Bind("CallTime") %>'></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("CallTime") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Comments">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtComments" runat="server" BorderStyle="None" Height="63px"
                                         MaxLength="300" Text='<%# Bind("Comments") %>' TextMode="MultiLine"
                                         Width="218px" Font-Names="Arial" Font-Size="14px"></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended By">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditFaculty" runat="server" Font-Size="14px"
                                         SelectedValue='<%# Eval("FacultyID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="lblFname" runat="server" Text='<%# Bind("FName") %>'></asp:Label>
                                     <asp:Label ID="lblMName" runat="server" Text='<%# Bind("MName") %>'></asp:Label>
                                     <asp:Label ID="lblLName" runat="server" Text='<%# Bind("LName") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended At">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditLocation" runat="server" Font-Size="14px"
                                         SelectedValue='<%# Eval("LocationID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label3" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField ShowHeader="False">
                                 <EditItemTemplate>
                                     <asp:CheckBox ID="chkIncoming" runat="server" Checked='<%# Eval("Incoming") %>'
                                         Font-Size="14px" Text="Incoming Call" />
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                         CommandName="Delete" Text="Delete"></asp:LinkButton>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:CommandField ShowEditButton="True" />
                         </Columns>
                         <HeaderStyle CssClass="ProfileMiddle" HorizontalAlign="Left" />
                         <AlternatingRowStyle CssClass="ProfileAlternateRow" Wrap="False" />
                     </asp:GridView>

コードビハインド:

VB
Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then

            If gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = True Then
                e.Row.ForeColor = Drawing.Color.Black
                e.Row.BackColor = Drawing.Color.FromArgb(51, 204, 51)
            ElseIf gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = False Then
                e.Row.ForeColor = Drawing.Color.White
                e.Row.BackColor = Drawing.Color.FromArgb(230, 19, 19)
            End If


            Dim ddlFaculty As DropDownList = CType(e.Row.FindControl("ddlEditFaculty"), DropDownList)
            Dim ddlLocation As DropDownList = CType(e.Row.FindControl("ddlEditLocation"), DropDownList)

            Dim myUtil As New Utility
            myUtil.FillFaculty(ddlFaculty)
            myUtil.FillLocations(ddlLocation)

        End If

    Catch ex As Exception
    End Try
End Sub

ddlFaculty と ddlLocation は、行から取得しようとしても何もありません。

解決策 3

RowDataBound イベントの EditItemTemplate 内のコントロールを取得できます。

Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
Try
        If e.Row.RowType = DataControlRowType.DataRow Then
          If e.Row.RowState = DataControlRowState.Edit Then
                DropDownList ddl = GridView1.Rows          [e.RowIndex].FindControl("ddlEditFaculty") as DropDownList; 
          End If
        End If
Catch ex As Exception
End Try
End Sub

これが正しい場合は、回答としてマークされます。

解決策 1

ユーザーが編集モードでデータを更新したときに起動される適切なイベント ハンドラーを追加できます (たとえば、 RowUpdating) を使用してコントロールを見つけます。 RowIndex 現在更新中の対応する行の。 つまり、次のようなものになります (アイデアを示すために C# コードを使用しました)。

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("ddlEditFaculty") as DropDownList;
}

解決策 5

Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) gvCallHistory.RowDataBound を処理します
試す
e.Row.RowType = DataControlRowType.DataRow の場合
e.Row.RowState = DataControlRowState.Edit の場合
DropDownList ddl = GridView1.Rows [e.RowIndex].FindControl(“ddlEditFaculty”) を DropDownList として使用します。
終了の場合
終了の場合
元を例外としてキャッチ
試行を終了する
エンドサブ

コメント

タイトルとURLをコピーしました