Why my method call 3 times?

hadi tag asked on October 6, 2015 14:41

hi,

i create a webpart in kentico 8.1 that contain a button and in the click event of button put a "EventLogProvider.LogException". when i click on button and check event log application, I saw that my method run 3 times(3 record in event log). While I do not have any loop in my web part Code.

please help me...

Recent Answers


Brenden Kehren answered on October 6, 2015 14:45

How many times are you using that webpart on the same or other templates on that page load? Also where are you performing your button click events? Can you share some pseudo code?

0 votesVote for this answer Mark as a Correct answer

hadi tag answered on October 6, 2015 14:53

now i use it as follows in transformation:

<%@ Register Src="~/CMSWebParts/MyWebParts/DownloadButton.ascx" TagName="DownloadControl" TagPrefix="htz" %>

<htz:DownloadControl ID="DownloadControl1" runat="server" EcGuid='<%# Eval("Guid").ToString() %>' WithAuthentication="true" WithCaptcha="true" WithPayment="false" FileCost="1000" />

and my click event :

protected void downloadBtn_Click(object sender, EventArgs e)
{
    EventLogProvider.LogException("00000000000", "CMSWebParts_MyWebParts_DownloadButton", new Exception("ex: "));
    if (WithCaptcha && captchaElem.Visible)
    {
        if (captchaElem.IsValid())
        {
            if (string.IsNullOrEmpty(ItemId))
            {
                CustomTableItem cti = CustomTableItem.New("customtable.Status");
                cti.SetValue("UserIp", HttpContext.Current.Request.UserHostAddress);
                cti.SetValue("UserGuid", MembershipContext.AuthenticatedUser.UserGUID);
                cti.SetValue("UserName", MembershipContext.AuthenticatedUser.UserName);
                cti.Insert();
            }
        }
    }
}
0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on October 6, 2015 15:03

Is this only happening while you view your page in the cmsdesk or on the front end/live site as well?

0 votesVote for this answer Mark as a Correct answer

hadi tag answered on October 6, 2015 15:12 (last edited on October 6, 2015 15:13)

this is only happening while i view my page in the live site.

my complete code :

protected void downloadBtn_Click(object sender, EventArgs e)
{
    EventLogProvider.LogException("00000000000", "CMSWebParts_MyWebParts_DownloadButton", new Exception("ex: "));
    if (WithCaptcha && captchaElem.Visible)
    {
        if (captchaElem.IsValid())
        {
            if (string.IsNullOrEmpty(ItemId))
            {
                CustomTableItem cti = CustomTableItem.New("customtable.Status");
                cti.SetValue("UserIp", HttpContext.Current.Request.UserHostAddress);
                cti.SetValue("UserGuid", MembershipContext.AuthenticatedUser.UserGUID);
                cti.SetValue("UserName", MembershipContext.AuthenticatedUser.UserName);
                cti.SetValue("UserEmailAddress", MembershipContext.AuthenticatedUser.Email);
                cti.SetValue("PageAlias", HttpContext.Current.Request.RawUrl.Substring(firstIndex, lastIndex - firstIndex));
                cti.SetValue("DownloadCount", 1);
                cti.Insert();
            }
            else
            {
                var existing = CustomTableItemProvider.GetItem(int.Parse(ItemId), "customtable.Status");
                var currentDownloadCount = ValidationHelper.GetInteger(existing.GetValue("DownloadCount", -1), -1);
                if (currentDownloadCount <= MaxDownloadCount)
                {
                    existing.SetValue("DownloadCount", currentDownloadCount + 1);
                    existing.Update();
                }
                else
                {
                    return;
                }
            }

            var documentFile = CustomTableItemProvider.GetItems("customtable.Document")
                .TopN(1)
                .WhereEquals("FileNameGuid", EcGuid);
            if (!DataHelper.DataSourceIsEmpty(documentFile))
            {
                DownloadFile(documentFile.Tables[0].Rows[0]["DocFilePath"].ToString(),
                    documentFile.Tables[0].Rows[0]["FileNameGuid"].ToString() + ".pdf");
            }
        }
        else
        {
            SecurityCodeMessage.Text = "error100";
        }
    }
    else
    {
        if (string.IsNullOrEmpty(ItemId))
        {
            CustomTableItem cti = CustomTableItem.New("customtable.Status");
            cti.SetValue("UserIp", HttpContext.Current.Request.UserHostAddress);
            cti.SetValue("UserGuid", MembershipContext.AuthenticatedUser.UserGUID);
            cti.SetValue("UserName", MembershipContext.AuthenticatedUser.UserName);
            cti.SetValue("UserIsRegister", !MembershipContext.AuthenticatedUser.IsPublic());
            cti.SetValue("PageAlias", HttpContext.Current.Request.RawUrl.Substring(firstIndex, lastIndex - firstIndex));
            cti.SetValue("DownloadCount", 1);
            cti.Insert();
        }
        else
        {
            var existing = CustomTableItemProvider.GetItem(int.Parse(ItemId), "customtable.Status");
            var currentDownloadCount = ValidationHelper.GetInteger(existing.GetValue("DownloadCount", -1), -1);
            if (currentDownloadCount <= MaxDownloadCount)
            {
                existing.SetValue("DownloadCount", currentDownloadCount + 1);
                existing.Update();
            }
            else
            {
                return;
            }
        }

        var documentFile = CustomTableItemProvider.GetItems("customtable.Document")
            .TopN(1)
            .WhereEquals("FileNameGuid", EcGuid);
        if (!DataHelper.DataSourceIsEmpty(documentFile))
        {
            DownloadFile(documentFile.Tables[0].Rows[0]["DocFilePath"].ToString(),
                documentFile.Tables[0].Rows[0]["FileNameGuid"].ToString() + ".pdf");
        }
    }
}
0 votesVote for this answer Mark as a Correct answer

hadi tag answered on October 6, 2015 15:17

In this section, instead of (+1), 3 times to be added :

existing.SetValue("DownloadCount", currentDownloadCount + 1);
0 votesVote for this answer Mark as a Correct answer

hadi tag answered on October 6, 2015 15:32

i use from this control in my transformation of repeater Which repeats custom table record

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on October 6, 2015 18:46

Ok, so you are using this control in a transformation? It would be called for each item in the transformation, so could that be the reason? It is going to run that for each of the items that is loaded in. You may want to put it somewhere else or add a conditonal clause to only do it if it is the first item.

1 votesVote for this answer Mark as a Correct answer

hadi tag answered on October 7, 2015 08:56

I found problem

When the file download starts, "Internet download manager" has sent several requests to the server that it caused my method called 3 times.

Now, how do I solve this problem?؟

0 votesVote for this answer Mark as a Correct answer

Yang Yu answered on October 5, 2016 08:52

may I ask how you solve this problem?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.