Control.Init Control.Init Control.Init Control.Init Event

定义

当服务器控件初始化时发生;初始化是控件生存期的第一步。Occurs when the server control is initialized, which is the first step in its lifecycle.

public:
 event EventHandler ^ Init;
public event EventHandler Init;
member this.Init : EventHandler 
Public Custom Event Init As EventHandler 

示例

下面的示例将一个自定义事件处理程序Calendar_InitInit事件的Calendar控件。The following example assigns a custom event handler, Calendar_Init, to the Init event of a Calendar control.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Calendar_Init(object sender, EventArgs e)
  {
    // Do any related intialization work.
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Control Init Event Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Calendar ID="Calendar1"
                     runat="server" 
                     OnInit="Calendar_Init"/>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Calendar_Init(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Do any related intialization work.
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Control Init Event Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Calendar ID="Calendar1"
                     runat="server" 
                     OnInit="Calendar_Init"/>
    </div>
    </form>
</body>
</html>

注解

服务器控件应执行创建和设置实例所需的任何初始化步骤。Server controls should perform any initialization steps that are required to create and set up an instance. 不能使用此事件; 中的视图状态信息不尚未填充。You cannot use view-state information within this event; it is not populated yet. 在此情况下,无论是子级或到此控件的父级期间,不应访问另一个服务器控件。You should not access another server control during this event, regardless of whether it is a child or parent to this control. 其他服务器控件不能确定要创建并可供访问。Other server controls are not certain to be created and ready for access. 服务器控件事件的详细信息,请参阅ASP.NET Web 窗体服务器控件事件模型For more information on server control events, see ASP.NET Web Forms Server Control Event Model.

适用于

另请参阅