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...
$(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" %>
$(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);
}
});
});
runat="server" DescriptionUrl='<%# Eval("FileID") %>'
AlternateText='<%# Eval("FileName") %>' ToolTip="Download File"
onclick="imgbtn_Click" />
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)
{
}
}
}
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kiresh GangariyaPosted Jun 17, 2013, 1:05 AM
Sorry for inconviency its my mistake
its RowCommand
try this
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
ashutosh kumar guptaPosted Jun 17, 2013, 12:58 AM
Thanks for response but there is no OnItemCommand Event is there in the GridView.
Kiresh GangariyaPosted Jun 17, 2013, 12:45 AM
instead of using imgbtn_Click Event you should use OnItemCommand Event
below is HTML
an here is your code
if(e.CommandName == "DownloadFlie")
{
//Do your Stuffs
}
Let me know if it works
Thanks