首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用RowCommand/RowDeleting事件从文件夹/数据库中删除图像

问使用RowCommand/RowDeleting事件从文件夹/数据库中删除图像
EN

Stack Overflow用户
提问于 2012-03-05 23:00:31
回答 2查看 3.9K关注 0票数 1

我正在上传图像并将它们显示在Gridview.I中保存图像在文件夹中和名称在database.Now中如果我上传了3张图片并不得不删除其中一张我有图片1 2 3.当我选择select.So 2 than this in deleted..How时,我可以这样做吗?这是我上传图片的代码:

代码语言:javascript
复制
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["baza_chestionar"].ToString());
con.Open();

SqlCommand cmd = new SqlCommand("Insert into Images(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();

con.Close();
Response.Redirect("~/upload.aspx");
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-05 23:19:16

您可以使用下面的GridView事件来删除图像。

  1. RowCommand
  2. RowDeleting

代码隐藏

代码语言:javascript
复制
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    grd.Rows[e.RowIndex].FindControl("Control of the file Name"); 
    //Find control that contains file Name
    if (System.IO.File.Exists("FilePath"))
    {
        File.Delete(Path.Combine("path", "FileName"));
    }
    //Your Delete Code to delete record from database
}

protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    grd.Rows[e.RowIndex].FindControl("Control of the file Name"); 
    //Find control that contains file Name
    if (System.IO.File.Exists("FilePath"))
    {
        File.Delete(Path.Combine("path", "FileName"));
    }
    //Your Delete Code to delete record from database
}

HTML

代码语言:javascript
复制
<asp:GridView ID="grd" runat="server" onrowcommand="grd_RowCommand" 
onrowdeleting="grd_RowDeleting">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="delete" runat="server" Text="Select" CommandName="delete"></asp:LinkButton> 
                <asp:LinkButton ID="deleteRow" runat="server" Text="Select" CommandName="deleteRow"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

请注意-您可以使用RowCommand/RowDeleting Events...and,我将使用RowDeleting

票数 1
EN

Stack Overflow用户

发布于 2012-03-05 23:03:36

要删除文件,请使用System.IO.File.Delete

代码语言:javascript
复制
File.Delete(Path.Combine(path, filename));

其余的实现细节,如何通知服务器要删除的适当文件,由您决定。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9568789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档