从asp.net代码后面的文件中注入Javascript

时间:2022-08-25 22:09:36

Am I injecting this correctly?

我正确地注射了这个吗?

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

//Verify script isn't already registered
if (!ClientScript.IsClientScriptBlockRegistered(myScriptName))
{
    Response.Write('b');
    myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
    myScript += "alert('hi');";
    myScript += "\n\n </script>";

    ClientScript.RegisterClientScriptBlock(this.GetType(), myScriptName, myScript);
}

This is in my Page_Load, but I never see an alert and I have no JavaScript errors either.

这是在我的Page_Load中,但我从未看到警报,也没有JavaScript错误。

4 个解决方案

#1


46  

You can use registerstartupscript instead of registerclientscriptblock!

您可以使用registerstartupscript而不是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

RegisterStartupScript使用RegisterStartupScript时,它将在页面中的所有元素之后(在表单的结束标记之前)呈现脚本。这使脚本可以调用或引用页面元素,而不会在页面的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

RegisterClientScriptBlock使用RegisterClientScriptBlock时,脚本将在Viewstate标记之后,但在任何页面元素之前呈现。由于这是一个直接脚本(不是可以调用的函数,它将立即由浏览器执行。但是浏览器在此阶段没有在Page的DOM中找到标签,因此您应该收到“未找到对象”错误

Difference between registerstartupscript and registerclientscriptblock

registerstartupscript和registerclientscriptblock之间的区别

protected void Page_Load(object sender, System.EventArgs e)
 {
      string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        myScript += "alert('hi');";
        myScript += "\n\n </script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
 }

#2


5  

I have a feeling this is related to your asp.net/html markup.

我觉得这与你的asp.net/html标记有关。

Do you have a form tag like so in your .aspx file?

你的.aspx文件中是否有这样的表单标签?

<form id="form1" runat="server">
   ....
</form>

#3


5  

Both RegisterStartupScript and RegisterClientScriptBlock will work.

RegisterStartupScript和RegisterClientScriptBlock都可以工作。

Problem lies in myScript (string variable).In myScript variable you need to use alert variable only, as whenever you use this, script tag will be added automatically to your page's HTML at runtime. To check this right on your page and see the source of the page.

问题在于myScript(字符串变量)。在myScript变量中,您只需要使用警报变量,因为每当您使用它时,脚本标记将在运行时自动添加到页面的HTML中。要在您的页面上进行检查,请查看页面的来源。

protected void Page_Load(object sender, EventArgs e)
{
        string myScript = string.Empty;

        //myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        string registerKey = "alert('RegisterClientScriptBlock');";
        myScript = "alert('RegisterStartupScript');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterStartupScript", myScript, true);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientScriptBlock", registerKey, true);
    }

Note: I have executed RegisterStartupScript first and than RegisterClientScriptBlock.But RegisterStartupScript alert will be executed at last, as it will be added at the end of the page. RegisterClientScriptBlock will always be added at the starting of the page.

注意:我首先执行RegisterStartupScript而不是RegisterClientScriptBlock.But最后将执行RegisterStartupScript警报,因为它将在页面末尾添加。 RegisterClientScriptBlock将始终在页面的开头添加。

#4


3  

You should use RegisterStartupScript.

您应该使用RegisterStartupScript。

#1


46  

You can use registerstartupscript instead of registerclientscriptblock!

您可以使用registerstartupscript而不是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

RegisterStartupScript使用RegisterStartupScript时,它将在页面中的所有元素之后(在表单的结束标记之前)呈现脚本。这使脚本可以调用或引用页面元素,而不会在页面的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

RegisterClientScriptBlock使用RegisterClientScriptBlock时,脚本将在Viewstate标记之后,但在任何页面元素之前呈现。由于这是一个直接脚本(不是可以调用的函数,它将立即由浏览器执行。但是浏览器在此阶段没有在Page的DOM中找到标签,因此您应该收到“未找到对象”错误

Difference between registerstartupscript and registerclientscriptblock

registerstartupscript和registerclientscriptblock之间的区别

protected void Page_Load(object sender, System.EventArgs e)
 {
      string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        myScript += "alert('hi');";
        myScript += "\n\n </script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
 }

#2


5  

I have a feeling this is related to your asp.net/html markup.

我觉得这与你的asp.net/html标记有关。

Do you have a form tag like so in your .aspx file?

你的.aspx文件中是否有这样的表单标签?

<form id="form1" runat="server">
   ....
</form>

#3


5  

Both RegisterStartupScript and RegisterClientScriptBlock will work.

RegisterStartupScript和RegisterClientScriptBlock都可以工作。

Problem lies in myScript (string variable).In myScript variable you need to use alert variable only, as whenever you use this, script tag will be added automatically to your page's HTML at runtime. To check this right on your page and see the source of the page.

问题在于myScript(字符串变量)。在myScript变量中,您只需要使用警报变量,因为每当您使用它时,脚本标记将在运行时自动添加到页面的HTML中。要在您的页面上进行检查,请查看页面的来源。

protected void Page_Load(object sender, EventArgs e)
{
        string myScript = string.Empty;

        //myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        string registerKey = "alert('RegisterClientScriptBlock');";
        myScript = "alert('RegisterStartupScript');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterStartupScript", myScript, true);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientScriptBlock", registerKey, true);
    }

Note: I have executed RegisterStartupScript first and than RegisterClientScriptBlock.But RegisterStartupScript alert will be executed at last, as it will be added at the end of the page. RegisterClientScriptBlock will always be added at the starting of the page.

注意:我首先执行RegisterStartupScript而不是RegisterClientScriptBlock.But最后将执行RegisterStartupScript警报,因为它将在页面末尾添加。 RegisterClientScriptBlock将始终在页面的开头添加。

#4


3  

You should use RegisterStartupScript.

您应该使用RegisterStartupScript。