无法在其Codebehind中访问BasePage的Body标记

时间:2015-10-01 20:39:28

标签: c# html asp.net

我正在尝试向网站中的所有网页添加Javascript。因此,我创建了一个基页,网站上的大多数页面都是使用<@Reference标记从此页面继承的。这是我的基页样本

<%@ Page Language="C#" CodeBehind="BasePage.aspx.cs" Inherits="MyProj.BasePage" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>
</head>
<body  ID="BasePage_Body" runat="server">
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

在basepage.aspx.cs中我使用如下..

   protected override void OnInit(EventArgs e)
            {
               base.OnInit(e);
    //Adding this Client Side script for my Page
                if (<someCondition>)
                {
                    BasePage_Body.Attributes.Add("onunload", "HandleClose()");
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "MWGBrowserhandlingClose",
                        "<script language=\"JavaScript\">" +
                        "function HandleClose() {" +
                              "alert(\"Killing the session on the server!!\");" +
                              "PageMethods.AbandonSession();" +
                                "} </script>");
                }
}

但是我得到了BasePage_Body的Nullreference异常。有关此为何以及如何解决的任何输入。

提前致谢

0 个答案:

没有答案