从后面的代码把javascript放在页面上

时间:2011-12-02 15:25:18

标签: javascript asp.net

我还有一些其他的javascript函数正在我正在使用的文本框的onfocus和onblur事件上设置。在这些函数中,它调用与任何控件无关的通用javascript函数。我想知道如何简单地将这个函数从后面的代码中吐出到页面的html中。像这样......

Page.ClientScript.RegisterStartupScript(this.GetType(), "?????", getCounter);
编辑:这就是我的意思

public class MVADTextBox : TextBox
        {
 protected override void OnLoad(EventArgs e)
            {
 var getCounter = "<script language=\"javascript\">" +
                                "function GetCounter(input) {" +
                                    //this function gets the number of special characters taht are in a row.
                                    //it is only the grouping of characters that are right after your current position
                                    "var textbox = document.getElementById(input.id);" +
                                    "var mask = textbox.getAttribute('Mask');" +
                                    "var inputCharacters = textbox.getAttribute('InputCharacters');" +
                                    "var tbid = \"#\" + input.id;" +
                                    "var position = $(tbid).caret().start;" +
                                    "var counter = 0;" +
                                    "for (var i = position; i < mask.length; i++) {" +
                                    "     if (mask[i] != '#') {" +
                                    "        counter++;" +
                                    "        if (mask[i + 1] == '#') {" +
                                    "            break;" +
                                    "        }" +
                                    "    }" +
                                    "}" +
                                    "return counter;" +
                               " }" +
                            "</script>";

                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnFocus", onFocus);
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "GetCounter(input)", getCounter);



var onBlur = "<script language=\"javascript\"> function PopulateField(input) {if (input.value == \"\") {input.value = input.defaultValue; input.className = 'sampleText'; } } </script>";


  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnFocus", onFocus);

  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnBlur", onBlur);
    }
}

将on blur方法发送到页面。

4 个答案:

答案 0 :(得分:5)

<强>答案:

我认为Page.ClientScript已被弃用。您应该使用ClientScriptManager

将您的"?????"替换为脚本名称。老实说,脚本的名称是 几乎 无用(除非你需要稍后检查它的存在)。

ClientScriptManager.RegisterStartupScript(this.GetType(), "myCount", getCounter);

用法说明:

//You must surround your code with script tags when not passing the bool param
ClientScriptManager.RegisterStartupScript(this.GetType(),
                   "myCount",
                   "<script>alert('Hey')</script>");

// The last param tells .Net to surround your
// code with script tags (true) or not (false)
ClientScriptManager.RegisterStartupScript(this.GetType(),
                   "myCount",
                   "alert('Hey')", true);

其他信息:

来自MSDN的签名:

public void RegisterStartupScript(
    Type type,
    string key,
    string script
)

public void RegisterStartupScript(
    Type type,
    string key,
    string script,
    bool addScriptTags
)

请参阅:http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

答案 1 :(得分:1)

您可以将实际的函数定义放在getCounter中。请注意,正如James指出的那样,您当前拥有"????"的第二个参数是脚本的,它必须是为此类型注册的所有其他脚本中唯一的。第三个参数是脚本本身,第四个参数确定是否要添加脚本标记,这些标记必须为false,因为您已经添加了它们。

Page.ClientScript.RegisterStartupScript(this.GetType(), 
      "someKeyForThisType", getCounter, false);

答案 2 :(得分:1)

编辑:

 var getCounter = "<script language=\"javascript\">" +
                                        "function GetCounter(input) {" +
            //this function gets the number of special characters taht are in a row.
            //it is only the grouping of characters that are right after your current position
                                            "var textbox = document.getElementById(input.id);" +
                                            "var mask = textbox.getAttribute('Mask');" +
                                            "var inputCharacters = textbox.getAttribute('InputCharacters');" +
                                            "var tbid = \"#\" + input.id;" +
                                            "var position = $(tbid).caret().start;" +
                                            "var counter = 0;" +
                                            "for (var i = position; i < mask.length; i++) {" +
                                            "     if (mask[i] != '#') {" +
                                            "        counter++;" +
                                            "        if (mask[i + 1] == '#') {" +
                                            "            break;" +
                                            "        }" +
                                            "    }" +
                                            "}" +
                                            "return counter;" +
                                       " }" +
                                    "</script>";

        this.TextBox1.Attributes.Add("OnFocus", "GetCounter(this);");
        if (!ClientScript.IsClientScriptBlockRegistered("getCounter")) {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "getCounter", getCounter, false);
        }

答案 3 :(得分:1)

我认为您需要使用ClientScriptManager.RegisterClientScriptBlock方法

Try this

相关问题