Click here to Skip to main content
12,623,776 members (28,599 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

22.9K views
2 bookmarked
Posted

Stopping the multiple postbacks of ASP.NET linkbutton

, 18 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Stopping the multiple postbacks of ASP.NET linkbutton using simple JavaScript

Introduction

Assume Link Button is present on the web form. The problem is when the user has clicked on a Link Button that is causing the browser to post to the server, it will take some time for doing the operation in the server side (assume during post back, it will insert some records into database). So, in the mean time, the user has clicked the link button again and again, then those many times the page will get submitted to the server, so those many records will be inserted into the database. But the thing is whenever the user clicked on the Link button multiple times also I want to posted back to the server first time only but not on the subsequent requests. For this, generally what we do is once the client side validation is done, at the end of validation, we will disable the link button to prevent multiple submissions, but this does not work, i.e., even after disabling the link button from client side, if we click link button, it will do server side action. How can we resolve this problem? In order to resolve the above problem, we can do the following method.


Using the Code

Aspx Page

<![CDATA[<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>]]>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Link Button Click Example</title>
</head>
<script type="text/javascript" language="javascript">
    function Testing()
     {
       try
        {
            var retunboolean = true;
            var ele = document.getElementById('<%=linkButtonTesting.ClientID%>');
            
            if (ele != null && !ele.disabled)
                retunboolean = true;
            else
                retunboolean = false;
                
            if (ele != null)
                ele.disabled = true;
        }
        catch (err)
        {
            alert(err.description);
        }
         return retunboolean;
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:linkbutton id="linkButtonTesting" runat="server" onclientclick="return Testing();" onclick="linkButtonTesting_Click" xmlns:asp="#unknown">Check me</asp:linkbutton>
    </div>
    </form>
</body>
</html>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void linkButtonTesting_Click(object sender, EventArgs e)
    {
       //do some operation...
    }
}

Initially link button is enabled. Once we click on this, it will be disabled and JavaScript function will return true, so the server side action will happen. But for the next clicks onwords, I check whether the link button is disabled or not. If it is disabled, it means that it is not the first time request, then this JavaScript function will return false. So, as a restult of this, the server side action won't happen.

Points of Interest

if we click on the link button continuously, then those many times it will do postback, not only that even after disabling the linkbutton that if clicked on it then too it will do postback but using the above JavaScript, we can restrict the postback from the second time onwards.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionDoesn't work in IE9 Pin
andrey_er24-Mar-15 22:06
memberandrey_er24-Mar-15 22:06 
QuestionThank you Pin
bispenho25-Apr-14 11:05
memberbispenho25-Apr-14 11:05 
QuestionAmazinggggggggggggg Pin
Maher Elsayed13-May-13 8:59
memberMaher Elsayed13-May-13 8:59 
GeneralFormatting... Pin
Sandeep Mewara5-Sep-10 3:39
mentorSandeep Mewara5-Sep-10 3:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 18 Sep 2010
Article Copyright 2010 by Member 4532519
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid