2012-02-10 54 views
0

我想在我的GridView的OnRowDeleted事件中FindControl()一个HiddenField。。GridView OnRowDeleted上的FindControl()

protected void gridviewPersonPhoto_RowDeleted(object sender, GridViewDeletedEventArgs e) 
{ 
    //string strFileName = (the name of the file that needs to be deleted by System.IO 
    //which i will know after I FindControl() the HiddenField in the deleted row that contains the name) 
} 


<asp:GridView id="gridviewPersonPhoto" DataKeyNames"PersonPhotoId" 
    DataSourceId="sqldatasourcePersonPhoto" OnRowDeleted="gridviewPersonPhoto_RowDeleted"> 
    <TemplateField> 
     <ItemTemplate> 
      <asp:HiddenField ID="hiddenfieldFileName value='<%#Eval("FileName")%>' /> 
      <asp:Button ID="buttonDelete" CommandName="Delete" /> 
     </ItemTemplate> 
     <ItemTemplate> 
      <asp:Image ID="imgPhoto" ImageUrl='<%# "~/pub/photo/"+(Eval("FileName")) %>' runat="server" /> 
     </ItemTemplate> 
    </TemplateField> 
</asp:GridView> 
+0

你可以给和html代码吗? – Aristos 2012-02-10 06:09:21

+0

@Aristos,添加html/aspx – 2012-02-10 06:18:04

回答

2

在Row_Deleted事件中尝试此代码。

GridViewRow gvRow= (GridViewRow)(((Button)e.CommandSource).NamingContainer); 
HiddenField HF = gvRow.FindControl("hiddenfieldFileName") as HiddenField; 
+0

这工作,但我很快找到了一个更简单的解决方案:string FileName = e.Values [0] .ToString();但它需要我使用Bind而不是Eval。 – 2012-02-10 09:57:20