Gridview and jquery: Jquery code is not working properly

Jun 16 2013 4:07 AM
Hi,

I have a grid-view in which few items are there i.e. FIleID, FileName, CreatedOn, CreatedBy, DownloadCount and Download and there is a link button in the download column. I wand whenever any user download the file the counter of download should be increased by 1. I have written a jquery code which is as follows...


 <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $("#<%=GridView1.ClientID%> td").click(function () {
                var colIndex = $(this).index();
                alert(colIndex);
                if (colIndex == 5) {
                    var downloadcount = $(this).parent().children().eq(4).text();
                    $(this).parent().children().eq(4).text(parseInt(downloadcount) + 1);
                }
            });
        });

Now the problem is whenever I click on the download image button the counter is increased by 1 and the file is downloaded but when I click on the download column then the the counter is increased by 1. Here is the problem. Please help me ASAP.

WebForm2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="GridView_Jquery.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $("#<%=GridView1.ClientID%> td").click(function () {
                var colIndex = $(this).index();
                alert(colIndex);
                if (colIndex == 5) {
                    var downloadcount = $(this).parent().children().eq(4).text();
                    $(this).parent().children().eq(4).text(parseInt(downloadcount) + 1);
                }
            });
        });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="FileID" HeaderText="FileID" />
            <asp:BoundField DataField="FileName" HeaderText="FileName" />
            <asp:BoundField DataField="CreatedOn" HeaderText="CreatedOn" />
            <asp:BoundField DataField="ModifiedOn" HeaderText="ModifiedOn" />
            <asp:BoundField DataField="Downloads" HeaderText="Downloads" />
            <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtn" ImageUrl="~/downloads.png" ClientIDMode="Static" ImageAlign="Top" 
                                runat="server" DescriptionUrl='<%# Eval("FileID") %>'  
                                AlternateText='<%# Eval("FileName") %>' ToolTip="Download File" 
                                onclick="imgbtn_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

WebForm2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace GridView_Jquery
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private DataTable ShowFile(string strEmail)
        {
            SqlConnection objConnection = null;
            SqlCommand objCommand = null;
            SqlDataAdapter objDataAdapter = null;
            DataTable objDataTable = null;
            SqlCommandBuilder objCommandBuilder = null;
            try
            {
                objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
                if (objConnection.State == ConnectionState.Closed)
                {
                    objConnection.Open();
                }
                objCommand = new SqlCommand("SP_GetFilesByUser", objConnection);
                objCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = strEmail;
                objCommand.CommandType = CommandType.StoredProcedure;
                objDataAdapter = new SqlDataAdapter(objCommand);
                objDataTable = new DataTable();
                objCommandBuilder = new SqlCommandBuilder(objDataAdapter);
                objDataAdapter.Fill(objDataTable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objConnection.Dispose();
                objCommand.Dispose();
                objDataAdapter.Dispose();
                objCommandBuilder.Dispose();
            }
            return objDataTable;
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = ShowFile("[email protected]");
                GridView1.DataBind();
            }
        }

        protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}


Answers (3)