错误的OnClientClick事件 - 处理条款要求WithEvents就 [英] Error onclientclick event - Handles clause requires WithEvents

查看:192
本文介绍了错误的OnClientClick事件 - 处理条款要求WithEvents就的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有事件的OnClientClick这是给我这个错误此关闭按钮。它工作在一些网页和anothers不起作用。我想不通为什么。有一个在code页面把手后蓝色的下划线。

 < ASP:按钮的ID =EditButton=服务器的CausesValidation =FALSE的CommandName =编辑文本=编辑的CssClass =btnclass/>
< ASP:按钮的ID =btnClose=服务器文本=关闭的CssClass =btnclass的OnClientClick =btnClose_Click()/>保护小组btnClose_Click(发送者为对象,E作为EventArgs的)把手btnClose.Click
    Me.Dispose()
    的Response.Redirect(〜/ SearchForm.aspx,FALSE)
结束小组


解决方案

的OnClientClick 是当您点击客户端的按钮应该执行的JavaScript code -侧。但你处理 Button.Click -event这是一个服务器端的事件。

因此​​,这个按钮需要与创建 WithEvents就 (在VB.NET)如果你在设计中添加一个按钮,自动完成。

所以一定有某个地方在codebehind类(或 aspx.designer.vb 文件)这样的:

 昏暗的WithEvents就btnClose作为新按钮

您还可以通过添加 <$ ASPX页面上,而不是事件处理程序C $ C>把手

 &LT; ASP:按钮的ID =btnClose=服务器文本=关闭的CssClass =btnclass
    的OnClick =btnClose_Click
    的OnClientClick =btnClose_ClientClick()/&GT;

注意 btnClose_ClientClick 是js- code可以是一个js功能。顺便说一句,如果你想prevent从客户端返回的回发的OnClientClick

 的OnClientClick =返回btnClose_ClientClick();

(presuming的 btnClose_ClientClick 返回布尔

I have this close button with onclientclick event which is giving me this error. It works in some pages and doesn't work in anothers. I cannot figure out why. There is a blue underline after Handles in the code page.

<asp:Button ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" CssClass="btnclass" />
<asp:Button ID="btnClose" runat="server" Text="Close" CssClass="btnclass" OnClientClick="btnClose_Click()" />

Protected Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    Me.Dispose()
    Response.Redirect("~/SearchForm.aspx", False)
End Sub

解决方案

OnClientClick is the javascript code that should be executed when you click on the button on client-side. But you are handling the Button.Click-event which is a serverside event.

Therefore this button needs to be created with WithEvents (in VB.NET) which is done automatically if you add a button in the designer.

So there must be somewhere in the codebehind class (or in the aspx.designer.vb file) this:

Dim WithEvents btnClose As New Button

You can also add the event handler on the aspx page instead of via Handles:

<asp:Button ID="btnClose" runat="server" Text="Close" CssClass="btnclass" 
    OnClick="btnClose_Click" 
    OnClientClick="btnClose_ClientClick()" />

Note that btnClose_ClientClick is the js-code which can be a js-function. By the way, if you want to prevent the postback from client-side return false from OnClientClick:

OnClientClick="return btnClose_ClientClick();" 

( presuming that btnClose_ClientClick returns a bool )

这篇关于错误的OnClientClick事件 - 处理条款要求WithEvents就的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆