4

I want to disable a LinkButton clink on the client site.

objLinkButton.disabled = true;
// or 
objLinkButton.disabled = -1;

This disables the link but I am still able to click on the link and do PostBack.

Is there any way I can disable the link.

Code:

<asp:linkbutton id="xyz" runat="server"
                onClick="javascript:LinkDisable(this)" ></asp:linkbutton>

which renders as a link which does a postback... I am opening the page on postback in a new window. What I want to do is.. when I click on the link for the firsttime.. it will open a new page and then it will disable the link.

what I am doing is .. onClick of that link I have a javascript function.. which is something like this..

In LinkDisable ...

function LinkDisable(obj)
{
obj.disabled = -1;
obj.href = '#';
//Cant return false from here.. otherwise it wont postback...
}

When I do this.. the link gets grey's out ... but I am still able to click it. I want to stop the user from clicking it the second time.

Any help is appreciated.

1
  • Can you provide a code sample? Commented Apr 16, 2009 at 2:23

7 Answers 7

3

If you want to disable a linkbutton, just use following code.

Markup

<asp:LinkButton ID="lnkButton" Text="Submit" runat="server">
</asp:LinkButton>

C# Code

this.lnkButton.Attributes.Add("disabled","disabled");
Sign up to request clarification or add additional context in comments.

Comments

1

The simplest way to disable a link (and render it like normal text) without using jQuery is to remove it's href attribute entirely.

For example here is the rendered link:

<a id='link1' href="javascript:disableLink('link1');">Click me</a>

And the required JavaScript:

function disableLink(id) {
   document.all[id].removeAttribute('href');
}       

This works for me in both IE and Firefox, but you may wish to do some more extensive testing.

Comments

1

Using jQuery you can do this in a browser-independent either by replacing the href or by adding a click handler that preempts and stops the link being followed.

$('#objLinkButton').attr('href','#').addClass('disabled-link');

or

$('#objLinkButton').click( function() { return false; } )
                   .addClass('disabled-link');

Where the disabled-link class has some CSS to change the look of the link so that it looks disabled visually.

Note that if this if the control is inside a naming container (like a GridView or UserControl), you'll have to reference the name using the "ending with" selector on the id.

$('id$="objLinkButton"')...

EDIT: Based on your update. Try this:

var code = null;
$(document).ready( function() {
    var button = $('#objLinkButton');
    code = button.attr('href').replace(/javascript:/,''); // save postback function
    button.attr('href','#'); // replace postback function
    button.click( function() {
        $(this).unbind('click'); // get rid of click handler so it only fires once
        if (code) { // if link hasn't been used
            eval(code);  // do post back
        }
    });
}); 

6 Comments

Thanks for the solution but I just need a basic solution. Not using Jquery as of now. I need to star
You can do this without jQuery, but frankly jQuery is pretty basic and anything else would be just reimplementing part of jQuery. Once you have a little bit of javascript and start trying to make it browser-neutral, you'll find that investing a little bit in learning and using a framework pays off.
I understand your point tavnfosson. Appreciate your help. I still cant figure out how to fix this issue.. just disabling and making it return false wont work for me .. becuase I have to click the link the first time and then disable it. I will try to fix it somehow... as of now I dont know the fix
code will again be reset to NULL right.. if you do postback as the whole page is rendered again. Correct me if I am wrong.
I understood you to say that the postback was happening in another window. If this isn't correct, we've wasted a lot of time. You could just remove the LinkButton server side and replace it with a Literal.
|
1

I am building on tvanfosson 's answer. His answer using jQuery works well, but if you have validators it messes things up. Here goes:

var code = null;
$(document).ready(function () {
    var button = $('#SubmitPaymentLnkBtn');
    code = button.attr('href').replace(/javascript:/, ''); 
    button.attr('href', '#'); // replace postback function
    button.click(function () {
        if (Page_ClientValidate('PaymentInfo')) {
                           // now only runs if above validationGroup is valid
            $(this).unbind('click'); 
            if (code) {
                $(this).addClass('disabled-btn');
                eval(code);  // do post back
            } 
        }
    });
});

Then in your markup, be sure to set ClientIDMode to Static.

<asp:LinkButton ID="SubmitPaymentLnkBtn" Text=" submit " runat="server" CssClass="BlackBgText" ValidationGroup="PaymentInfo" Font-Size="22px"  onclick="SubmitPaymentLnkBtn_Click" ClientIDMode="Static" />

Comments

0

This is an edit based on the comment you posted to allow the linkbutton to be followed through on the first click but not otherwise.

To allow the link to be clicked the first time, but not after that, create a variable on the page to keep track of the click.

var clicked = 0;

Since linkbuttons produce hrefs on the front end, you could do this

<a href="#" OnClientClick="return false"></a>

OnClientClick is specific to .NET

If you want to do it after the page loads in certain situations only:

<a href="" id="linkbutton">

var linkbutton = document.getElementById("linkbutton");
linkbutton.onclick = function(){
   if(clicked != 0){
      //if other than 0, not followed
      return false;
   }
   else{
      //link is followed here since it's the first time it's being clicked and clicked value = 0
      clicked = clicked + 1;
   }
}

4 Comments

Thanks for your reply. Cant do this... becuase this will not allow the link to be clicked even the first time. I want the link to be clicked first time and open the link in a new window. but after that i.e second click I want to disable it i.e not clickable.
Hi Dhana, this solution will work for a normal link, not when the link is created by LinkButton. What happens is ..in case of linkbutton it does a postback. So the variable you use for keeping track gets reset everytime you do a postback.I cant touch the server code in this situation.
Forgot about the postback thing didn't I! Another hackish way would be to assign it to a cookie, that would be persistent.
Thanks Dhana... i take my comment back.. as I am posting back and opening in a new window Ty
0

Found the Solution guys... these javascript solution will work but if you refresh the page... it will clear the varaibles.

So thats a bug right there..

Found the solution using "userData" ... IE Session data... it really cool.

Checkout this link:http://www.eggheadcafe.com/articles/20010615.asp

Comments

0

Try this. Simplest Example

<asp:LinkButton ID="LinkButton6" runat="server" disabled="disabled" OnClientClick="return false;"> Test Button</asp:LinkButton>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.