te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>c# - Disabling LinkButton doesn't disable the click event in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Disabling LinkButton doesn't disable the click event in javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Apr 16, 2009 at 4:50 tvanfosson 533k102 gold badges700 silver badges799 bronze badges asked Apr 16, 2009 at 2:08 BenBen 7434 gold badges11 silver badges18 bronze badges 1
  • Can you provide a code sample? – bendewey Commented Apr 16, 2009 at 2:23
Add a ment  | 

7 Answers 7

Reset to default 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");

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.

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
        }
    });
}); 

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" />

This is an edit based on the ment 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;
   }
}

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./articles/20010615.asp

Try this. Simplest Example

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

评论列表(0)

  1. 暂无评论