简体   繁体   English

禁用回发<ASP:LinkButton>

[英]Disable the postback on an <ASP:LinkButton>

I have an ASP.NET linkbutton control on my form.我的表单上有一个 ASP.NET 链接按钮控件。 I would like to use it for javascript on the client side and prevent it from posting back to the server.我想在客户端将它用于 javascript 并防止它回发到服务器。 (I'd like to use the linkbutton control so I can skin it and disable it in some cases, so a straight up tag is not preferred). (我想使用链接按钮控件,以便我可以在某些情况下为其设置外观并禁用它,因此不推荐使用直接标记)。

How do I prevent it from posting back to the server?如何防止它回发到服务器?

ASPX code: ASPX 代码:

<asp:LinkButton ID="someID" runat="server" Text="clicky"></asp:LinkButton>

Code behind:后面的代码:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        someID.Attributes.Add("onClick", "return false;");
    }
}

What renders as HTML is:呈现为 HTML 的内容是:

<a onclick="return false;" id="someID" href="javascript:__doPostBack('someID','')">clicky</a>

In this case, what happens is the onclick functionality becomes your validator.在这种情况下,onclick 功能将成为您的验证器。 If it is false, the "href" link is not executed;如果为false,则不执行“href”链接; however, if it is true the href will get executed.然而,如果它是真的,href 将被执行。 This eliminates your post back.这消除了您的回帖。

This may sound like an unhelpful answer ... But why are you using a LinkButton for something purely client-side?这听起来可能是一个无用的答案......但是为什么您将 LinkBut​​ton 用于纯粹的客户端? Use a standard HTML anchor tag and set its onclick action to your Javascript.使用标准的 HTML 锚标记并将其onclick操作设置为您的 Javascript。

If you need the server to generate the text of that link, then use an asp:Label as the content between the anchor's start and end tags.如果您需要服务器生成该链接的文本,则使用asp:Label作为锚点的开始和结束标记之间的内容。

If you need to dynamically change the script behavior based on server-side code, consider asp:Literal as a technique.如果您需要根据服务器端代码动态更改脚本行为,请考虑将asp:Literal作为一种技术。

But unless you're doing server-side activity from the Click event of the LinkButton, there just doesn't seem to be much point to using it here.但是,除非您从 LinkBut​​ton 的 Click 事件执行服务器端活动,否则在这里使用它似乎没有多大意义。

You can do it too你也可以

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

And it stop the link button postback它停止链接按钮回发

只需设置 href="#"

<asp:LinkButton ID="myLink" runat="server" href="#">Click Me</asp:LinkButton>

Just been through this, the correct way to do it is to use:刚刚经历过这个,正确的方法是使用:

  1. OnClientClick
  2. return false

as in the following example line of code:如以下示例代码行所示:

<asp:LinkButton ID="lbtnNext" runat="server" OnClientClick="findAllOccurences(); return false;" />

I think you should investigate using a HyperLink control.我认为您应该使用 HyperLink 控件进行调查。 It's a server-side control (so you can manipulate visibility and such from code), but it omits a regular ol' anchor tag and doesn't cause a postback.它是一个服务器端控件(因此您可以从代码中操纵可见性等),但它省略了常规的 ol' 锚标记并且不会导致回发。

在 C# 中,你会做这样的事情:

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");

Instead of implement the attribute:而不是实现属性:

public partial class _Default : System.Web.UI.Page{
 protected void Page_Load(object sender, EventArgs e)
 {
    someID.Attributes.Add("onClick", "return false;");
 }}

Use:采用:

OnClientClick="return false;"

inside of asp:LinkButton tag在 asp:LinkBut​​ton 标签内

To avoid refresh of page, if the return false is not working with asp:LinkButton use为避免页面刷新,如果return false不适用于asp:LinkButton使用

href="javascript: void;"

or要么

href="#"

along with OnClientClick="return false;"连同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.以上代码将调用浏览器打印而不刷新页面。

如果您想保留滚动位置,您可以做的其他事情是:

<asp:LinkButton runat="server" id="someId" href="javascript: void;" Text="Click Me" />

在 onclick 事件上调用 java 脚本函数。

Have you tried to use the OnClientClick ?您是否尝试过使用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>

In the jquery ready function you can do something like below -在 jquery 就绪函数中,您可以执行以下操作 -

var hrefcode = $('a[id*=linkbutton]').attr('href').split(':');
var onclickcode = "javascript: if`(Condition()) {" + hrefcode[1] + ";}";
$('a[id*=linkbutton]').attr('href', onclickcode);

No one seems to be doing it like this:似乎没有人这样做:

createEventLinkButton.Attributes.Add("onClick", " if (this.innerHTML == 'Please Wait') { return false; } else {  this.innerHTML='Please Wait'; }");

This seems to be the only way that works.这似乎是唯一有效的方法。

Why not use an empty ajax update panel and wire the linkbutton's click event to it?为什么不使用一个空的 ajax 更新面板并将链接按钮的点击事件连接到它? This way only the update panel will get updated, thus avoiding a postback and allowing you to run your javascript这样只会更新更新面板,从而避免回发并允许您运行 javascript

You might also want to have the client-side function return false.您可能还希望客户端函数返回 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);"我使用 clickable 类来设置指针、颜色等内容,使其外观类似于锚标记,但我不必担心它会被发回或必须执行 href="javascript:void (0);" trick.诡计。

使用 html 链接而不是 asp 链接,您可以在 html 链接之间使用标签进行服务器端控制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM