我有一个Javascript函数,可以扩展ASP Ajax Accordion控件的所有窗格。功能是:
function expandEditAll() {
var behavior = $get("<%= accEditIncident.ClientID %>").AccordionBehavior;
for (var i = 0; i < behavior._panes.length; i++) {
behavior.get_Pane(i).content.style.display = 'inline';
behavior.get_Pane(i).content.height = behavior.get_Pane(i).content.scrollHeight;
behavior.get_Pane(i).content.style.height = behavior.get_Pane(i).content.scrollHeight + 'px';
}
}
当我用以下函数调用此函数时:
<a href="#" onclick="expandEditAll();">Expand All</a>
效果很好。
但是当我从C#代码中调用它时如下:
StringBuilder expand = new StringBuilder();
expand.AppendLine("<script type='text/javascript'>");
expand.AppendFormat(" expandEditAll();" + System.Environment.NewLine);
expand.AppendLine("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "expandEdit", expand.ToString(), false);
它不起作用,在Chrome Developer工具中我收到错误Uncaught TypeError: Cannot read property '_panes' of undefined
。当我进行完全相同的函数调用时,为什么behavior
未定义?
我也尝试过:
ClientScript.RegisterClientScriptBlock(this.GetType(), "expandEdit", expand.ToString(), false);
但得到错误Uncaught ReferenceError: $get is not defined
。
答案 0 :(得分:0)
试试这个,我能够执行javascript函数。
StringBuilder script = new StringBuilder();
script.Append(@"<script language='javascript'>");
script.Append(@"expandEditAll()");
script.Append(@"</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "expandEdit", script.ToString());