禁用上的回发
我的窗体上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
ASPX 代码:
代码隐藏:
呈现为 HTML 的内容是:
在这种情况下,发生的情况是 onclick 功能成为您的验证器。 如果为 false,则不执行“href”链接; 但是,如果为 true,则 href 将被执行。 这消除了您的回帖。
ASPX code:
Code behind:
What renders as HTML is:
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.
这听起来像是一个没有帮助的答案......但是为什么你要使用 LinkButton 来实现纯粹的客户端功能呢? 使用标准 HTML 锚标记并将其
onclick
操作设置为您的 Javascript。如果您需要服务器生成该链接的文本,请使用
asp:Label
作为锚点的开始标记和结束标记之间的内容。如果您需要根据服务器端代码动态更改脚本行为,请考虑使用
asp:Literal
作为一种技术。但除非您通过 LinkButton 的 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.
你也可以这样做
它会停止链接按钮回发
You can do it too
And it stop the link button postback
只需设置 href="#"
Just set href="#"
我认为您应该研究使用 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.
刚刚经历过这个,正确的方法是使用:
OnClientClick
return false
,如以下示例代码行所示:
Just been through this, the correct way to do it is to use:
OnClientClick
return false
as in the following example line of code:
在 C# 中,你可以这样做:
In C#, you'd do something like this:
为了避免刷新页面,如果
return false
不能与asp:LinkButton
一起使用与
上面的
OnClientClick="return false;"
一起或 使用该代码将调用浏览器打印而不刷新页面。
To avoid refresh of page, if the
return false
is not working withasp:LinkButton
useor
along with
OnClientClick="return false;"
Above is code will call the browser print without refresh the page.
在 onclick 事件上调用 java 脚本函数。
call java script function on onclick event.
而不是实现属性:
Use:
inside of asp:LinkButton 标记
Instead of implement the attribute:
Use:
inside of asp:LinkButton tag
如果您想保留滚动位置,您还可以做其他事情:
Something else you can do, if you want to preserve your scroll position is this:
为什么不使用空的 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
您是否尝试过使用
OnClientClick
?Have you tried to use the
OnClientClick
?在 jquery Ready 函数中,您可以执行如下操作 -
In the jquery ready function you can do something like below -
您可能还希望客户端函数返回 false。
您可能还会考虑:
我使用 clickable 类来设置指针、颜色等内容,使其外观类似于锚标记,但我不必担心它会被发回或必须执行 href ="javascript:void(0);" 诡计。
You might also want to have the client-side function return false.
You might also consider:
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.
使用 html 链接而不是 asp 链接,您可以在服务器端的 html 链接之间使用标签
控制
use html link instead of asp link and you can use label in between html link for server side
control
似乎没有人这样做:
这似乎是唯一有效的方法。
No one seems to be doing it like this:
This seems to be the only way that works.