Inject Javascript from asp.net code behind files

后端 未结 4 1309
盖世英雄少女心
盖世英雄少女心 2020-12-08 15:32

Am I injecting this correctly?

string myScriptName = \"EventScriptBlock\";
string myScript = string.Empty;

//Verify script isn\'t already registered
if (!Cl         


        
4条回答
  •  抹茶落季
    2020-12-08 16:16

    You can use registerstartupscript instead of registerclientscriptblock!

    RegisterStartupScript When you use RegisterStartupScript, it will render your script after all the elements in the page (right before the form's end tag). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM

    RegisterClientScriptBlock When you use RegisterClientScriptBlock, the script is rendered right after the Viewstate tag, but before any of the page elements. Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error

    Difference between registerstartupscript and registerclientscriptblock

    protected void Page_Load(object sender, System.EventArgs e)
     {
          string myScript = "\n";
         Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
     }
    

提交回复
热议问题