简体   繁体   English

从标记中调用codebehind方法并抛出javascript错误

[英]Calling codebehind method from markup and is throwing javascript error

I have a div tag that has a click event and the method I'm trying to call is from the codebehind. 我有一个带有click事件的div标记,而我尝试调用的方法来自后台代码。

This is my div tag 这是我的div标签

<div class="DivA" runat="server" id="ThisDiv" onclick="<%ClickMe();%>"></div>

The method is a simple 方法很简单

public void ClickMe()
    {
        Response.Redirect("www.google.ca");
    }

I'm just testing this before I add the real stuff to it. 在添加真正的东西之前,我只是在测试它。 The error that it is throwing is... 它引发的错误是...

JavaScript critical error at line 16, column 49 in http://localhost:24307/DIVPAGE.aspx

SCRIPT1002: Syntax error

this is the line that it is giving me 这就是它给我的那条线

<div id="ThisDiv" class="DivA" onclick="&lt;%ClickMe();%>"></div>

I have tried changing the 我试图改变

 <%ClickMe();%> 

to

 <%=ClickMe()%>

But that throws the same error. 但这引发了同样的错误。 Another thing I don't understand is when you look at the line with the error that it is missing the runat tag and has added other characters to the onclick event. 我不明白的另一件事是,当您看到以下行时,该行缺少runat标记,并已向onclick事件添加了其他字符。

Thanks 谢谢

You have a concept problem here, do this, and test it will work: 您在这里有一个概念问题,请执行此操作,然后测试它是否可以工作:

<asp:LinkButton id="lbClickMe" runat="server" OnClick="ClickMe">
   <div class="DivA" id="ThisDiv">
      The Click Me Button!
   </div>
</asp:LinkButton>

That's it, when runat=server is specified ASP.NET page parser will process the element as server side, so for this elements/controls no server tags in markup are allowed except data binding tags inside control templates. 就是这样,当指定runat = server时,ASP.NET页解析器将把该元素作为服务器端进行处理,因此,对于此元素/控件,除控件模板内的数据绑定标签外,不允许标记中的任何服务器标签。 So to call you method you have to put a runat server on a control that haves the Click event, this is the case of the LinkButton, inside of him you can put your div for some specific styling of your UI. 因此,要调用您的方法,您必须将一台Runat服务器放在具有Click事件的控件上(LinkBut​​ton就是这种情况),在他的内部,您可以将div用于UI的某些特定样式。

Also not that, if you really want to have the your div behaving like that, there is no problem in complicating what is simple, but in that case please do this instead: 同样不是这样,如果您真的想让div表现得如此,那么简单化就没有问题了,但是在这种情况下,请这样做:

<asp:LinkButton id="lbClickMe" runat="server" OnClick="ClickMe" Visible="False"></asp:LinkButton>
<div class="DivA" id="ThisDiv" onclick="<%= Page.GetPostBackEventReference(lbClickMe) %>"></div>

The GetPostBackEventReference extracts the javascript code necessary to simulate your link button click, but once more is preferable to use directly the link button if you can. GetPostBackEventReference提取模拟链接按钮单击所必需的javascript代码,但如果可以的话,最好还是直接使用链接按钮。

Hope it helps, 希望能帮助到你,

Regards. 问候。

You can't call server-side C# methods from the DOM like that. 您不能像这样从DOM调用服务器端C#方法。 You can only call JavaScript functions in an HTMLElement's onclick handler. 您只能在HTMLElement的onclick处理程序中调用JavaScript函数。

It is correct that you can call server-side methods using the template language, however this will be executed at the time of rendering the page; 可以使用模板语言调用服务器端方法是正确的,但是这将在呈现页面时执行。 you could, for example, render the results of that server-side method, but you can't use a server-side method as a handler for a client-side event. 例如,您可以呈现该服务器端方法的结果,但不能将服务器端方法用作客户端事件的处理程序。 The onclick event on a DOM element can only call a JavaScript function. DOM元素上的onclick事件只能调用JavaScript函数。

ASP web controls also have an OnClick event attribute, which is probably what's confusing you; ASP Web控件还具有OnClick事件属性,这可能会让您感到困惑。 this is different from the onclick event attribute on DOM elements (ASP will create additional code for its web controls, eg in case of an asp:button). 这与DOM元素上的onclick事件属性不同(ASP将为其Web控件创建其他代码,例如,在asp:button的情况下)。 This works using ViewState and a postback to the server. 使用ViewState和回发到服务器可以正常工作。 The onclick event for a DOM element however won't do those things for you. 但是,DOM元素的onclick事件不会为您做这些事情。

Adding runat="server" will convert your element to an ASP control, however it will only be an HtmlControl. 添加runat="server"会将您的元素转换为ASP控件,但是它只会是HtmlControl。 In the case of a <div> , it will be an HtmlGenericControl which simply writes out the onclick attribute of your element as it is. <div>的情况下,它将是HtmlGenericControl ,它只是按原样写出元素的onclick属性。

The <%= %> syntax emits a string, it doesn't do anything, like a redirect. <%= %>语法发出一个字符串,它不执行任何操作,如重定向。

You need to do your redirect client-side with this javascript: 您需要使用以下javascript进行重定向客户端:

  window.location = 'http://my.url.com';

If you need to interact with server side code, you need to do so with AJAX communicating to a web service to get the URL you need, and then performing the redirect described above. 如果需要与服务器端代码进行交互,则需要通过AJAX与Web服务进行通信来获取所需的URL,然后执行上述重定向。

Update 更新

Sorry lads, brain freeze. 对不起小伙子们,大脑冻结了。

Yes, indeed, you can inject a string that will be evaluated as a click handler, but the handler must be a javascript function, not a server-side one! 是的,的确,您可以注入一个将被视为单击处理程序的字符串,但是该处理程序必须是javascript函数,而不是服务器端函数! Once the page is rendered, it can no longer interact with the server save for communicating with a web service (or if we want to get technical, web sockets as well). 呈现页面后,除了与Web服务进行通信(或者,如果我们也想获得技术知识,也需要Web套接字)进行通信时,它就无法与服务器交互。

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

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