2011-12-02 67 views
1

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

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); 
    } 
} 

的上模糊的方法是越来越发送到页面。

+0

没有到目前为止为我工作已建议。 – joncodo

回答

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

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

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

这是不正确的。第二个参数是'key',而不是脚本。 –

+0

@詹姆斯 - 谢谢你,它是固定的。自从我使用这种方法已经太久了,我应该在回答之前进行更多的调查。 –

+0

不用担心,-1删除。你的意思是功能是由myfuntion提供的 –

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); 
     } 
+0

? – joncodo

+0

您已更改您的问题。我会相应地编辑。你可以发布你想要的功能文本框的名称/ ID吗? –

+0

在这种情况下,“myFunction”只是这个脚本块的别名,它*几乎*不重要你在那里写什么.. – MilkyWayJoe

1

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

Try this

相关问题