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:
$(document).ready(function () {
$("#<%=gvShowFiles.ClientID%>").tablesorter();
});
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;
}
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);
});
}
Your Files | ||
Font-Size="9pt" Font-Names="Arial" AutoGenerateColumns="False" RowStyle-CssClass="record" BorderColor="#DADADA" BorderStyle="Solid" BorderWidth="1px" onrowdatabound="gvShowFiles_RowDataBound"> HeaderStyle-Width="100"> HeaderStyle-Width="100"> runat="server" DescriptionUrl='<%# Eval("FileID") %>' AlternateText='<%# Eval("FileName") %>' OnClientClick="DownloadFile();" onclick="btnDownload_Click" /> |
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()");
//}
}
}
}

Sunny SharmaPosted Jun 4, 2013, 3:19 AM
to achive this, pass the button to the function and find the rowIndex:
send button as parameter like:
---------------------------------
OnClientClick="DownloadFile(this)"
----------------------------------
JS code to access the rowIndex:
---------------------------------
function DownloadFile(e){
var rowIndex = $(e).closest('tr').index() // now do anything with rowIndex that you want to.
... // rest of the code goes here
}
---------------------------------
Happy Coding :)
Don't forget to mark this as answer if it helps!
Thanks.
ashutosh kumar guptaPosted Jun 4, 2013, 2:08 AM
Please help me.
Sunny SharmaPosted Jun 3, 2013, 2:40 PM
Well, your code looks all fine. What causing problems to you in my opinion is Event Fallback. The code snippet below in the given code above is assigning a function to GridViewRow.onclick also. It's getting assigned on the very first invokation of DownloadFile() JS function.
--------------------------------------------------------------
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);
});
}
--------------------------------------------------------------
What's happening here is when you're clicking the imageButton, it is invoking the function "DownloadFile()", and right after the execution completes for "DownloadFile()" it is invoking gridview's "tr.onclick". This is why you're getting the increment Twice.
try the code below:
-----------------------------
--------------------------------------------------------------
function DownloadFile() {
var download = $(this).children().eq(4).html(); ;
alert("Ashutosh:"+download);
$(this).children().eq(4).text(parseInt(download) + 1);
}
--------------------------------------------------------------
Hope this helps.
Please write back for any clarification.
Thanks.