简体   繁体   English

ASP.NET AJAX服务器控件-检测控件的实例化

[英]ASP.NET AJAX Server Control - Detecting Instantiation of Control

I am creating a custom .NET AJAX Server Control, and need to get access to the JavaScript object functionality associated with that control. 我正在创建一个自定义.NET AJAX服务器控件,并且需要访问与该控件关联的JavaScript对象功能。 I can do this by finding the control in the ScriptManager using the $find method. 我可以通过使用$ find方法在ScriptManager中找到控件来做到这一点。 However, I need to determine when I can call $find. 但是,我需要确定何时可以调用$ find。 If I do this on the "onload" event of the body of my HTML page, it can't find the control. 如果我在HTML页面正文的“ onload”事件上执行此操作,则它将找不到控件。 Thus I end up having to locate the control with each event I wire up and my code ends up looking like this: 因此,我最终不得不在每次连接事件时都找到控件,并且我的代码最终看起来像这样:

function button1_click() {
    var control = $find("<%=Control.ClientID%>");
    control.DoSomething();
}

function button2_click() {
    var control = $find("<%=Control.ClientID%>");
    control.DoSomethingElse();
}

I would rather store that control once, and use it throughout the rest of my event calls. 我宁愿一次存储该控件,并在其余的事件调用中使用它。 Thus I'm hoping the code would eventually look something like this: 因此,我希望代码最终看起来像这样:

var _control = null;
function load() {
     _control = $find("<%=Control.ClientID%>");
}

function button1_click() {    
    _control.DoSomething();
}

function button2_click() {
    _control.DoSomethingElse();
}

Let me know if this doesn't make sense. 让我知道这是否没有道理。 I am new at creating these custom controls, so I'm not quite sure of the terminology yet. 我是创建这些自定义控件的新手,所以我还不确定该术语。 Thanks for your help! 谢谢你的帮助!

The "load" DOM event occurs before the ASP.NET Ajax client-side framework is initialized. 在初始化ASP.NET Ajax客户端框架之前,将发生“加载” DOM事件。 Client-side controls are initialized by handling the init event of the Sys.Application object. 通过处理Sys.Application对象的init事件来初始化客户端控件。 That's why an ASP.NET Ajax control's initialization script is output like: 这就是为什么输出ASP.NET Ajax控件的初始化脚本的原因:

Sys.Application.add_init(function() {
    $create( ... )
});

You can use the load event of the Sys.Application object or its shortcut- the pageLoad method. 您可以使用Sys.Application对象的加载事件或其快捷方式pageLoad方法。 It occurs after the init event and all ASP.NET Ajax controls will be initialized then. 它发生在init事件之后,然后将初始化所有ASP.NET Ajax控件。 Here is some sample code: 这是一些示例代码:

var _control = null;

function pageLoad() {
    _control = $find("<%= Control1.ClientID %>");
}

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

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