Disable the postback on an

前端 未结 17 1481
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 00:18

I have an ASP.NET linkbutton control on my form. I would like to use it for javascript on the client side and prevent it from posting back to the server. (I\'d like to use t

相关标签:
17条回答
  • 2020-12-03 01:01

    You can do it too

    ...LinkButton ID="BtnForgotPassword" runat="server" OnClientClick="ChangeText('1');return false"...

    And it stop the link button postback

    0 讨论(0)
  • 2020-12-03 01:02

    You might also want to have the client-side function return false.

    <asp:LinkButton runat="server" id="button" Text="Click Me" OnClick="myfunction();return false;" AutoPostBack="false" />
    

    You might also consider:

    <span runat="server" id="clickableSpan" onclick="myfunction();" class="clickable">Click Me</span>
    

    I use the clickable class to set things like pointer, color, etc. so that its appearance is similar to an anchor tag, but I don't have to worry about it getting posted back or having to do the href="javascript:void(0);" trick.

    0 讨论(0)
  • 2020-12-03 01:04

    To avoid refresh of page, if the return false is not working with asp:LinkButton use

    href="javascript: void;"
    

    or

    href="#"
    

    along with OnClientClick="return false;"

    <asp:LinkButton ID="linkPrint" runat="server" CausesValidation="False" href="javascript: void;"
            OnClientClick="javascript:self.print();return false;">Print</asp:LinkButton>
    

    Above is code will call the browser print without refresh the page.

    0 讨论(0)
  • 2020-12-03 01:05

    Have you tried to use the OnClientClick?

    var myLinkButton = new LinkButton { Text = "Click Here", OnClientClick = "JavaScript: return false;" };
    
    <asp:LinkButton ID="someID" runat="server" Text="clicky" OnClientClick="JavaScript: return false;"></asp:LinkButton>
    
    0 讨论(0)
  • 2020-12-03 01:06

    In the jquery ready function you can do something like below -

    var hrefcode = $('a[id*=linkbutton]').attr('href').split(':');
    var onclickcode = "javascript: if`(Condition()) {" + hrefcode[1] + ";}";
    $('a[id*=linkbutton]').attr('href', onclickcode);
    
    0 讨论(0)
  • 2020-12-03 01:08

    In C#, you'd do something like this:

    MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");
    
    0 讨论(0)
提交回复
热议问题