为什么此ClientScript会抛出未定义的错误

时间:2017-04-18 15:55:22

标签: javascript c# asp.net webforms

我有一个带有输入字段的控件,当用户点击回车时,我希望回发重定向到控件所在的页面,并滚动到页面上输入字段的位置。这是一个功能,当我没有以编程方式命名该功能时,它可以正常工作。它生成一个对使用它的控件唯一的函数,并将该函数添加到控件onkeypress

protected void KeepInView(HtmlInputControl anchor)
{

    string anchorName = anchor.ClientID;
    string defaultEnterScript = @"
                    function handle_{0}(e) {{
                        if (e.key === 'Enter')
                        {{
                            __doPostBack('{1}', '');
                        }}
                    }};";

    string scrollTo = @"
                        $(function(){{
                               console.log('scrolling');
                                var top = document.getElementById('ELEMENT').offsetTop;
                                window.scrollTo(0, top);
                         }});";

    ScriptManager.RegisterClientScriptBlock(this, GetType(), "defaultEnter", String.Format(defaultEnterScript,anchorName, anchorName), true);
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "scrollTo", String.Format(scrollTo), true);

    anchor.Attributes["onkeypress"] = String.Format("handle_{0}(event);", anchorName);
}

问题

当我按Enter键时,我得到错误handle_uniqueIdentifierName未定义。当我将函数名称从handle_{0}()更改为硬编码名称handle()时;函数已定义并且工作得很好,但是所有控件都具有相同的功能,这不是我想要的。如何在用户点击进入时定义我的函数。

0 个答案:

没有答案