2012-02-25 17 views
0

フォルダから画像を取得してデータリストに画像を表示します。 データリストの[削除]ボタンをクリックすると、フォルダ内の画像を削除します。データリストの削除ボタンを使用してフォルダから画像を削除

はここに私の削除ボタンのコードです:私は削除したい正確なイメージ名を取得する方法がわからない

protected void delete_onClick(object sender, EventArgs e) 
    { 

     string fileName = sender as string; 

     File.Delete(Server.MapPath(fileName)); 

     FileInfo fInfo; 

     fInfo = new FileInfo(fileName); 

     fInfo.Delete(); 

     gvImages.DataBind(); 
    } 

、各画像と削除ボタンがあります。あなたがボタンの上にコマンド名を使用する必要があります

<div> 
    <asp:DataList ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal" 
      runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true"> 

      <ItemTemplate> 
       <center> 
        <table> 
         <tr> 
          <td style="width: 90px; height: 90px"> 
           <img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px; 
            width: 100px;" /> 
           <br /> 
           <asp:Button ID="Delete" Height="22px" OnClick="delete_onClick" Width="100px" runat="server" 
            Text="Delete Picture" /><br /> 
          </td> 
         </tr> 
        </table> 
       </center> 
      </ItemTemplate> 

     </asp:DataList> 

</div> 

答えて

0

は、ここに私のデータリストです。また、DataListでOnDeleteCommandを使用する必要があります。

<div> 
     <asp:DataList OnDeleteCommand="Delete_Command" ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal" 
       runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true"> 

       <ItemTemplate> 
        <center> 
         <table> 
          <tr> 
           <td style="width: 90px; height: 90px"> 
            <img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px; 
             width: 100px;" /> 
            <br /> 
            <asp:Button ID="Delete" Height="22px" CommandName="Delete" Width="100px" runat="server" 
             Text="Delete Picture" /><br /> 
           </td> 
          </tr> 
         </table> 
        </center> 
       </ItemTemplate> 

      </asp:DataList> 

    </div> 

その後、

例えばホールドファイル名:

<asp:Button CommandArgument ='<%# Container.DataItem %>' /> 

その後、

public void Delete_Command(Object sender, DataListCommandEventArgs e) 
    { 
     //you can hold filename on Button's CommandArgument 
     string fileName = e.CommandArgument; 

     File.Delete(Server.MapPath(fileName)); 

     FileInfo fInfo; 

     fInfo = new FileInfo(fileName); 

     fInfo.Delete(); 

     gvImages.DataBind(); 
    } 
1

ネスティング機能あなたが行ったようなプログラミングは、次のとおりです。

File.Delete(Server.MapPath(fileName)); 

てみてください、このようなものです、あなたがデバッグするときに、あなたが削除しようとしているされているどのファイルを参照することができるようになります。また

string fileName = e.CommandArgument; 

fileName = Server.MapPath(fileName); 

File.Delete(fileName); 

、あなたがエラーを取得していますか?例外?コードの周りに例外ハンドラがないのはなぜですか?

+0

本当にありがとうございます。私のproblem.againを解決してください –

関連する問題