2012-02-25 58 views
0

我有一个.NET 4的网页,其中包含一个用户控件有以下:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 
</asp:ScriptManagerProxy> 
<asp:LinkButton ID="btnExpand" runat="server" Text="Expand..." ClientIDMode="AutoID" 
    OnClick="btnExpand_Click"></asp:LinkButton> 
<asp:Label ID="btnDummy" runat="server"></asp:Label> 
<asp:Panel ID="pnlMyPanel" runat="server"> 
    <p>content</p> 
</asp:Panel> 
<ajaxToolkit:CollapsiblePanelExtender ID="cpeMyPanel" runat="server" 
    TargetControlID="pnlMyPanel" Collapsed="True" ExpandControlID="btnDummy" 
    CollapseControlID="btnDummy" BehaviorID="cpe"> 
</ajaxToolkit:CollapsiblePanelExtender> 

当点击展开按钮,它执行以下代码:

protected void btnAddComment_Click(object sender, EventArgs e) 
{ 
    string script = "var cpeMyPanel = $find('" + cpeMyPanel.BehaviorID + "');\r\n" + 
        "if (cpeMyPanel != null)\r\n" + 
        " cpeMyPanel.expandPanel();\r\n" + 
        "else\r\n" + 
        " alert(cpeMyPanel is null');"; 
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), 
      "Expand", script, true); 
} 

当脚本在浏览器上运行时,我得到警报,但是$ find()总是返回null。最终目标是,当用户单击按钮时,我想在扩展面板之前在服务器上执行一些代码(例如准备一些内容)。

任何想法可能会失踪?有一个更好的方法吗?

由于提前,

回答

1

的的RegisterStartupScript在客户端构建面板之前执行(搜索$页面创建功能)。目标是在客户端实例化所有组件时推迟脚本的执行。 AJAX控件工具包使用此trick

(function() { 
    var fn = function() { 
     var ajaxControl = $find('AJAX control'); 
     // do something usefull 
    }; 

    Sys.Application.add_load(fn); 
})(); 

我有一个AJAX工具包,但最有可能出现的是,照顾有关脚本注册了一些辅助方法没有经验。我会从ToolkitScriptManager开始。

看到这个blog post当你感兴趣为什么这样一个复杂的解决方案是如此简单的任务需要:)。