0

我无法获得价值。我得到的是一个空白值或videoName =“”。如何获取 videoName 的值?在 rowdeleting 事件中,我使用 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

当您将来从 gridview 添加或删除列时,这很容易出错,您的代码将再次失败。我建议您在模板字段中为您的 MovieName 字段使用标签并FindControl用于查找您的标签。象素

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