2011-12-02 69 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

相關問題