1

我正在创建一个自定义 Web 控件,基本上扩展了 RadTextBox,因为它没有 onBlur() 事件。我在 .ascx 文件中添加了一个脚本。我正在尝试注册脚本,但它失败了。我不确定我在这里缺少什么。

<script language="javascript">
    function handleLostFocus(o) {
        aler(o.toString());
    }
</script>

public partial class MyTextBox : RadTextBox, IScriptControl
{
    private void RegisterScripts()
    {
        try
        {
            String csname1 = "handleLostFocus";
            Type cstype = this.GetType();

            String cstext1 = "alert('Hello World');";
            ScriptManager.RegisterStartupScript(this, cstype, csname1, cstext1, true);
        }
        catch (Exception ex)
        {

        }
    }

    public MyTextBox()
    {
        Attributes.Add("padding", "5px");
        Skin = "Office2007";
        Attributes.Add("onBlur", "handleLostFocus(this);");

        RegisterScripts();
    }
}
4

1 回答 1

2

您在类的构造函数中调用 RegisterScripts。您的控件尚未在构造函数的 Page 中,因此您无法为尚未在 Page 上的控件注册启动脚本。

我怀疑最好的地方是 Init 方法。您可以通过将构造函数中的调用替换为:

Init += (s,e) => { RegisterScripts(); };

(您也可以在您的控件上创建一个完整的 Init 方法,但这是简写版本)

于 2012-12-11T23:55:51.730 回答