Jquery code is executing two times on ImageButton click.

Jun 3 2013 1:15 PM
I have a grid view in which there are 7 columns, in which 2 are templet columns, in which I have Image button and hyper link.
on click image button and hyperlink i have called the javascript code. Hyperlink jquery code is working fine but on Image button click jquery code is executing two times.

On Hyperlink Click: It is deleting the file.

On ImaheButton Click: It is downloading the file and in crease the download counter by 1. Problem is here it is increasing the counter by 2 by executing two times.

Please help me ASAP.

ShowFiles_UC.ascx is code:

<script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=gvShowFiles.ClientID%>").tablesorter();
    }); 
</script>

<script type="text/javascript">
    function DeleteTheRow(fileid) 
    {
        var datastring = 'FileID=' + fileid;
        if (confirm("Do you want to delete this record?")) 
        {
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/DeleteFile",
                data: datastring,
                success: function () {
                        $("a.delbutton" + fileid).parents(".record").css("background-color", "lightgreen");
                        $("a.delbutton" + fileid).parents(".record").fadeOut(500, function () {
                        $("a.delbutton" + fileid).parents(".record").remove();
                    });
                }
            });
        }
        return false;
    }
</script>

<script type="text/javascript">
    function DownloadFile() {
        $("#<%=gvShowFiles.ClientID %> tr").click(function () {
            if (!this.rowIndex) return;

            var download = $(this).children().eq(4).html(); ;
            alert("Ashutosh:"+download);
            $(this).children().eq(4).text(parseInt(download) + 1);
                });
    }
</script>

<table style="border: solid 1px ; background-color: #d5d5d5; width: 100%;">
    <tr>
        <td colspan="3">
            <strong>Your Files</strong></td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:GridView ID="gvShowFiles" runat="server" CellPadding="4" GridLines="Horizontal"
            Font-Size="9pt" Font-Names="Arial" AutoGenerateColumns="False" RowStyle-CssClass="record"
              BorderColor="#DADADA" BorderStyle="Solid" BorderWidth="1px" 
                onrowdatabound="gvShowFiles_RowDataBound">
              <RowStyle CssClass="record"></RowStyle>
                <Columns>
                    <asp:BoundField HeaderText="File ID" DataField="FileID" HeaderStyle-Width="100">
<HeaderStyle Width="100px"></HeaderStyle>
                    </asp:BoundField>
                    <asp:BoundField HeaderText="File Name" DataField="FileName" 
                        HeaderStyle-Width="100">
<HeaderStyle Width="100px"></HeaderStyle>
                    </asp:BoundField>
                    <asp:BoundField HeaderText="Created On" DataField="CreatedOn" HeaderStyle-Width="100">
<HeaderStyle Width="100px"></HeaderStyle>
                    </asp:BoundField>
                    <asp:BoundField HeaderText="Modified On" DataField="ModifiedOn" HeaderStyle-Width="100">
<HeaderStyle Width="100px"></HeaderStyle>
                    </asp:BoundField>
                    <asp:BoundField HeaderText="Download" DataField="Downloads"
                        HeaderStyle-Width="100">
<HeaderStyle Width="100px"></HeaderStyle>
                    </asp:BoundField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ImageUrl="~/Images/download.png" ClientIDMode="Static" 
                                runat="server" DescriptionUrl='<%# Eval("FileID") %>'  AlternateText='<%# Eval("FileName") %>' OnClientClick="DownloadFile();" onclick="btnDownload_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                      <ItemTemplate>
                       <%--<a href="#" id='<%# Eval("FileID") %>' class="delbutton"> <img  border="0" src="../Images/delete.jpg" alt="Delete" /></a>--%>
                       <a href="javascript:void(0);" id="delete<%# Eval("FileID") %>" class="delbutton<%# Eval("FileID") %>" onclick="DeleteTheRow(<%# Eval("FileID") %>);"> <img  border="0" src="../Images/delete.jpg" alt="Delete" /></a>
                       </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </td>
    </tr>
</table>

ShowFiles_UC.ascx.cs is code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FileSystem.BusinessLogic;

namespace FileSystem.UI.UserControls
{
    public partial class ShowFiles_UC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (Session["UserName"] != null)
                    {
                        gvShowFiles.DataSource = InputAndOutput_WS.ShowFiles(Convert.ToString(Session["UserName"]));
                        gvShowFiles.DataBind();
                        gvShowFiles.UseAccessibleHeader = true;
                        gvShowFiles.HeaderRow.TableSection = TableRowSection.TableHeader;
                        UserDetails.httpResponse = Response;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to put a generic error message. 
            }
        }

        protected void btnDownload_Click(object sender, ImageClickEventArgs e)
        {
            int iResult = 0;
            try 
            {
                ImageButton imgDownload = (ImageButton)sender;
                //imgDownload.Attributes.Add("onclick", "alert('Ashutosh');");
                iResult = InputAndOutput_WS.DownloadFile(Convert.ToInt32(imgDownload.DescriptionUrl), imgDownload.AlternateText);
                if (iResult == 1)
                {
                    Response.Write("File Downloaded Successfully.");
                }
                else
                {
                    Response.Write("Failed to Downloaded the file.");
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to put a generic error message. 
            }
        }

        protected void gvShowFiles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //ImageButton btnDownload = sender as ImageButton;
            //if (e.Row.Cells[4].Text == btnDownload.AlternateText)
            //{
            //    e.Row.Attributes.Add("onclick", "DownloadTheFile()");
            //}
        }
    }
}

Answers (3)