ASP.net - Delete Image Photo From GridView
GridDemo.aspx
<script type="text/javascript">
function deleteConfirm(pubid) {
var result = confirm('Do You Really Want to Delete Record : ' + pubid + ' ?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
---------------------
<asp:GridView ID="gridView" DataKeyNames="GalleryId" runat="server"
AutoGenerateColumns="False" ShowFooter="True" HeaderStyle-Font-Bold="true"
onrowdeleting="gridView_RowDeleting"
OnRowDataBound="gridView_RowDataBound" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
onselectedindexchanged="gridView_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="GalleryId">
<ItemTemplate>
<asp:Label ID="txtGallId" runat="server" Text='<%#Eval("GalleryId") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ThumbnailImagePath") %>'></asp:Image>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryName">
<ItemTemplate>
<asp:Label ID="lblGallName" runat="server" Text='<%#Eval("GalleryName") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryDesc">
<ItemTemplate>
<asp:Label ID="lblGallDesc" runat="server" Text='<%#Eval("GalleryDesc") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ThumbnailImagePath">
<ItemTemplate>
<asp:Label ID="lblThumbnailImagePath" runat="server" Text='<%#Eval("ThumbnailImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryImagePath">
<ItemTemplate>
<asp:Label ID="lblGalleryImagePath" runat="server" Text='<%#Eval("GalleryImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" HeaderText="Action" SelectText="Select" ShowSelectButton="True" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete" Enabled="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle Font-Bold="True" BackColor="#003399" ForeColor="#CCCCFF"></HeaderStyle>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
GridDemo.aspx.cs
protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//string filePathThumb = Server.MapPath(gridView.DataKeys[e.RowIndex].Values["lblThumbnailImagePath"].ToString());
//string filePathGal = Server.MapPath(gridView.DataKeys[e.RowIndex].Values["lblGalleryImagePath"].ToString());
string filePathThumb = Server.MapPath(gridView.Rows[e.RowIndex].Cells[5].Text);
string filePathGal = Server.MapPath(gridView.Rows[e.RowIndex].Cells[6].Text);
if (File.Exists(filePathThumb))
{
File.Delete(filePathThumb);
}
if (File.Exists(filePathGal))
{
File.Delete(filePathGal);
}
int galid = General.ConvertToInt16(gridView.DataKeys[e.RowIndex].Values["GalleryId"]);
string result = _dsGalMast.DeleteGalleryMasterData(galid);
if (result == "PASS")
{
LoadPhotoAlbum();
MessageBoxShow("GalleryID: " + galid + "<br>Photo Deleted Successfully.!", "SUCCESS", "SHOW");
}
else
{
MessageBoxShow("Some Error.?" + result, "FAIL", "SHOW");
}
}
catch (Exception ex)
{
MessageBoxShow(ex.Message, "FAIL", "SHOW");
}
}
------------------
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int galid = General.ConvertToInt16(DataBinder.Eval(e.Row.DataItem, "GalleryId"));
Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete");
if (lnkbtnresult != null)
{
lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" + galid + "')");
}
}
}
--------------------
<script type="text/javascript">
function deleteConfirm(pubid) {
var result = confirm('Do You Really Want to Delete Record : ' + pubid + ' ?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
---------------------
<asp:GridView ID="gridView" DataKeyNames="GalleryId" runat="server"
AutoGenerateColumns="False" ShowFooter="True" HeaderStyle-Font-Bold="true"
onrowdeleting="gridView_RowDeleting"
OnRowDataBound="gridView_RowDataBound" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
onselectedindexchanged="gridView_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="GalleryId">
<ItemTemplate>
<asp:Label ID="txtGallId" runat="server" Text='<%#Eval("GalleryId") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ThumbnailImagePath") %>'></asp:Image>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryName">
<ItemTemplate>
<asp:Label ID="lblGallName" runat="server" Text='<%#Eval("GalleryName") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryDesc">
<ItemTemplate>
<asp:Label ID="lblGallDesc" runat="server" Text='<%#Eval("GalleryDesc") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ThumbnailImagePath">
<ItemTemplate>
<asp:Label ID="lblThumbnailImagePath" runat="server" Text='<%#Eval("ThumbnailImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryImagePath">
<ItemTemplate>
<asp:Label ID="lblGalleryImagePath" runat="server" Text='<%#Eval("GalleryImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" HeaderText="Action" SelectText="Select" ShowSelectButton="True" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete" Enabled="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle Font-Bold="True" BackColor="#003399" ForeColor="#CCCCFF"></HeaderStyle>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
GridDemo.aspx.cs
protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//string filePathThumb = Server.MapPath(gridView.DataKeys[e.RowIndex].Values["lblThumbnailImagePath"].ToString());
//string filePathGal = Server.MapPath(gridView.DataKeys[e.RowIndex].Values["lblGalleryImagePath"].ToString());
string filePathThumb = Server.MapPath(gridView.Rows[e.RowIndex].Cells[5].Text);
string filePathGal = Server.MapPath(gridView.Rows[e.RowIndex].Cells[6].Text);
if (File.Exists(filePathThumb))
{
File.Delete(filePathThumb);
}
if (File.Exists(filePathGal))
{
File.Delete(filePathGal);
}
int galid = General.ConvertToInt16(gridView.DataKeys[e.RowIndex].Values["GalleryId"]);
string result = _dsGalMast.DeleteGalleryMasterData(galid);
if (result == "PASS")
{
LoadPhotoAlbum();
MessageBoxShow("GalleryID: " + galid + "<br>Photo Deleted Successfully.!", "SUCCESS", "SHOW");
}
else
{
MessageBoxShow("Some Error.?" + result, "FAIL", "SHOW");
}
}
catch (Exception ex)
{
MessageBoxShow(ex.Message, "FAIL", "SHOW");
}
}
------------------
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int galid = General.ConvertToInt16(DataBinder.Eval(e.Row.DataItem, "GalleryId"));
Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete");
if (lnkbtnresult != null)
{
lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" + galid + "')");
}
}
}
--------------------
Comments
Post a Comment