在母版页我有以下标记
<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;导致编译错误
发布评论