禁用上的回发

发布于 2024-07-06 02:06:33 字数 153 浏览 13 评论 0原文

我的窗体上有一个 ASP.NET linkbutton 控件。 我想在客户端使用它的 javascript 并防止它回发到服务器。 (我想使用 linkbutton 控件,这样我就可以对其进行换肤并在某些情况下禁用它,因此不首选直接标记)。

如何防止它发回服务器?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

月亮邮递员 2024-07-13 02:06:33

ASPX 代码:

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

代码隐藏:

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

呈现为 HTML 的内容是:

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

在这种情况下,发生的情况是 onclick 功能成为您的验证器。 如果为 false,则不执行“href”链接; 但是,如果为 true,则 href 将被执行。 这消除了您的回帖。

ASPX code:

<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:

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

In this case, what happens is the onclick functionality becomes your validator. If it is false, the "href" link is not executed; however, if it is true the href will get executed. This eliminates your post back.

猫九 2024-07-13 02:06:33

这听起来像是一个没有帮助的答案......但是为什么你要使用 LinkBut​​ton 来实现纯粹的客户端功能呢? 使用标准 HTML 锚标记并将其 onclick 操作设置为您的 Javascript。

如果您需要服务器生成该链接的文本,请使用 asp:Label 作为锚点的开始标记和结束标记之间的内容。

如果您需要根据服务器端代码动态更改脚本行为,请考虑使用 asp:Literal 作为一种技术。

但除非您通过 LinkBut​​ton 的 Click 事件执行服务器端活动,否则在这里使用它似乎没有多大意义。

This may sound like an unhelpful answer ... But why are you using a LinkButton for something purely client-side? Use a standard HTML anchor tag and set its onclick action to your 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.

If you need to dynamically change the script behavior based on server-side code, consider asp:Literal as a technique.

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.

久随 2024-07-13 02:06:33

你也可以这样做

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

它会停止链接按钮回发

You can do it too

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

And it stop the link button postback

梦幻的心爱 2024-07-13 02:06:33

只需设置 href="#"

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

Just set href="#"

<asp:LinkButton ID="myLink" runat="server" href="#">Click Me</asp:LinkButton>
我一直都在从未离去 2024-07-13 02:06:33

我认为您应该研究使用 HyperLink 控件。 它是一个服务器端控件(因此您可以从代码中操纵可见性等),但它省略了常规的 ol' 锚标记,并且不会导致回发。

I think you should investigate using a HyperLink control. 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.

邮友 2024-07-13 02:06:33

刚刚经历过这个,正确的方法是使用:

  1. OnClientClick
  2. return false

如以下示例代码行所示:

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

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;" />
一萌ing 2024-07-13 02:06:33

在 C# 中,你可以这样做:

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

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

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");
顾挽 2024-07-13 02:06:33

为了避免刷新页面,如果 return false 不能与 asp:LinkBut​​ton 一起使用

href="javascript: void;"

href="#"

上面的 OnClientClick="return false;" 一起

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

或 使用该代码将调用浏览器打印而不刷新页面。

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.

放赐 2024-07-13 02:06:33

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

call java script function on onclick event.

一梦等七年七年为一梦 2024-07-13 02:06:33

而不是实现属性:

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:LinkBut​​ton 标记

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

星軌x 2024-07-13 02:06:33

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

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

Something else you can do, if you want to preserve your scroll position is this:

<asp:LinkButton runat="server" id="someId" href="javascript: void;" Text="Click Me" />
停滞 2024-07-13 02:06:33

为什么不使用空的 ajax 更新面板并将链接按钮的单击事件连接到它? 这样,只有更新面板会得到更新,从而避免回发并允许您运行 JavaScript

Why not use an empty ajax update panel and wire the linkbutton's click event to it? This way only the update panel will get updated, thus avoiding a postback and allowing you to run your javascript

深陷 2024-07-13 02:06:33

您是否尝试过使用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>

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>
冷心人i 2024-07-13 02:06:33

在 jquery Ready 函数中,您可以执行如下操作 -

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

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);
零度° 2024-07-13 02:06:33

您可能还希望客户端函数返回 false。

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

您可能还会考虑:

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

我使用 clickable 类来设置指针、颜色等内容,使其外观类似于锚标记,但我不必担心它会被发回或必须执行 href ="javascript:void(0);" 诡计。

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.

苏辞 2024-07-13 02:06:33

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

use html link instead of asp link and you can use label in between html link for server side
control

冬天旳寂寞 2024-07-13 02:06:33

似乎没有人这样做:

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

这似乎是唯一有效的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文