mlg74
mlg74

Reputation: 520

Need value of gridview row onrowdeleting

I can't get the value. What I do get is a blank value or videoName = " ". How can I get the value of videoName? In the rowdeleting event, I am using Rows(e.Index).Cells(2).Text to get the value but it is blank. Is there another way to get the field, "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> 

Upvotes: 0

Views: 1018

Answers (1)

Zo Has
Zo Has

Reputation: 13018

This is prone to erros when you add or delete a column from gridview in future your code would fail again. I suggest you use a label inside template field for your MovieName field & use FindControl to find your label. Aspx

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

Code behind

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

Updated I found that in your code you have the movieName Visibleproperty set to false. This will prevent it from rendering on page. You can either store values in DataKeys or remove visible=false to fetch desired value.

Upvotes: 2

Related Questions