0

値を取得できません。私が得るのは、空の値または videoName = " " です。videoName の値を取得するにはどうすればよいですか? 行削除イベントでは、Rows(e.Index).Cells(2).Text を使用して値を取得していますが、空白です。フィールド「videoname」を取得する別の方法はありますか?

Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)


    Dim videoName As String = gridview1.Rows(e.RowIndex).Cells(2).Text
    Dim val As String = videoName
    If File.Exists(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName) Then
        File.Delete(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName)
    End If
End Sub

<asp:GridView id="GridView1" runat="server" Width="680px" GridLines="None" DataSourceID="SqlDataSource2" DataKeyNames="id" CellSpacing="1" CellPadding="3" BorderWidth="2px" BorderStyle="Ridge" BorderColor="White" AutoGenerateColumns="False"
               AllowPaging="True" AllowSorting="True" EmptyDataText="No Record Found" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="9" >
                <Columns>
                    <asp:BoundField HeaderText="Id" DataField="Id" ReadOnly="true" visible="false" />
                    <asp:BoundField HeaderText="CustomerID" DataField="CustomerID" ReadOnly="true" visible="false"/>
<asp:BoundField HeaderText="VideoName" DataField="VideoName" ReadOnly="true" visible="false"/>
                    <asp:TemplateField>
                        <HeaderStyle Width="5%" />
                        <ItemStyle Width="5%" />  
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this video?');"
                                CommandName="Delete">Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>              
                    <asp:TemplateField HeaderText="Clip" SortExpression="ThumbName">  
                        <HeaderStyle Width="5%" />
                        <ItemStyle Width="5%" />                          
                        <ItemTemplate>                                    
                            <div style="margin:2px; width:133px; background-color:rgb(68,68,68); -moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6); -webkit-box-shadow:5px 5px 5px rgba(68,68,68,0.6);box-shadow:5px 5px 5px rgba(68,68,68,0.6); zoom: 1;">
                                <asp:hyperlink id="link" NavigateUrl='<%# Eval("VideoName", "~/Test/playVideos2.aspx?FileName={0}&Thumb=" + Eval("ThumbName") + "&Duration=" + Eval("Duration"))%>' runat="server">
                                    <asp:image id="img" ImageUrl='<%# String.Format("~/contents/thumbs/{0}",Eval("ThumbName"))%>' width="130" height="80" runat="server" />
                                </asp:hyperlink>  
                                <asp:Label ID="lblMovieName" Text='<%#Bind("VideoName") %>' runat="server"></asp:Label>                                          
                            </div>                                                     
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date">
                        <HeaderStyle Width="15%" />
                        <ItemStyle Width="15%" />  
                        <ItemTemplate>
                           <asp:Label ID="date" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label><br />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderStyle Width="75%" />
                        <ItemStyle Width="75%" />  
                        <ItemTemplate>

                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            </asp:GridView> 
4

1 に答える 1

2

将来、グリッドビューから列を追加または削除すると、コードが再び失敗する可能性があります。MovieName フィールドのテンプレート フィールド内にラベルを使用し、ラベルFindControlを見つけるために使用することをお勧めします。Aspx

<asp:TemplateField HeaderText="Movie Name" >
            <ItemTemplate>
                <asp:Label ID="lblMovieName" Text='<%#Bind("MovieName") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

コードビハインド

 protected void gvCustomer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblMovieName = gvCustomer.Rows[e.RowIndex].FindControl("yourLableControlName") as Label;
        // Perform your delete
    }

更新 されましたが、コードで movieNameVisibleプロパティが に設定されていることがわかりましたfalse。これにより、ページにレンダリングされなくなります。値を格納するDataKeysか、 visible=false を削除して、目的の値を取得できます。

于 2013-04-23T04:04:07.423 に答える