身体RUNAT ="服务器"导致编译错误 [英] body runat="server" causing compilation error

查看:129
本文介绍了身体RUNAT ="服务器"导致编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在母版页我有以下标记

<body id="body" runat="server">

我已经设置 =服务器因为我需要能够访问body元素在code-落后。

I have set runat="server" because I need to be able to access the body element in code-behind.

我现在想一个JavaScript函数调用添加到身体的onload 事件,像这样:

I would now like to add a JavaScript function call to the body onload event, like this:

<body id="body" runat="server" onload="someJavaScriptFunction();">

然而,这是给我的一个编译错误,用一条消息无法解析符号someJavaScriptFunction();。如果我运行该应用程序我得到一个错误,告诉我​​

However, this is giving my a compile error, with a message of "Cannot resolve symbol someJavaScriptFunction();". If I run the application I get an error telling me

Compiler Error Message: CS1026: ) expected

这是怎么回事? 的onload 是一个客户端事件,那么,为什么这个ASP.NET的编译器护理?

What is going on here? onload is a client-side event, so why does the ASP.NET compiler care about this?

推荐答案

您需要在code的后面添加此;

You need to add this in the code behind;

protected void Page_Load(object sender, EventArgs e)
{
     body.Attributes.Add("onload", "someJavaScriptFunction();");
}

添加 =服务器标签使得它的服务器标签,哪怕是不明确prefixed者之一(如&LT; ASP:面板/&GT; )。在服务器上的标签,任何 onXXXX 事件处理程序处理服务器端事件,而不是客户端事件(除了当客户显式调用出来,如用的OnClientClick对于按钮)。

Adding runat="server" to a tag makes it a server tag, even if it isn't one of the explicitly prefixed ones (e.g. <asp:Panel />). On server tags, any onXXXX event handlers handle the server-side events, not the client-side events (except for when "client" is explicitly called out, such as with OnClientClick for buttons).

这篇关于身体RUNAT =&QUOT;服务器&QUOT;导致编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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