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.
-
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; } }
Ben : 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.Ben : 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.Dhana : Forgot about the postback thing didn't I! Another hackish way would be to assign it to a cookie, that would be persistent.Ben : Thanks Dhana... i take my comment back.. as I am posting back and opening in a new window Ty -
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 } }); });
Ben : Thanks for the solution but I just need a basic solution. Not using Jquery as of now. I need to startvanfosson : 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.Ben : 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 fixBen : 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.tvanfosson : 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.Ben : well not exactly, the post back will happen on the same page... but after postback it opens a new window.The current page still gets refreshed I guess. I tried your solution on a simple test app, and it does reset it... is another question I need to find out as to why the current page is gets refresBen : I take my comment back... You are right. Thanks!!!! -
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.
-
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
0 comments:
Post a Comment