22

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.

1
  • I have tried this in Chrome, IE, Safari and Firefox and it worked in all of them. Which browser are you using?
    – jdavies
    Aug 12, 2011 at 21:43

4 Answers 4

54

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<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        myScript += "alert('hi');";
        myScript += "\n\n </script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
 }
4
  • 1
    Do you know about asp.net page life cycle?
    – Pir Abdul
    Aug 12, 2011 at 22:00
  • 4
    There is an error in the provided code snippet above. The function RegisterStartupScript generates its own <script> tags, well at least for me it did. Removing these tags in the string myScript solved this. Dec 28, 2011 at 1:17
  • 2
    Agree with UW, the call to RegisterStartupScript pass true in last parameter which instruct .NET to add an script tag wrapper. Either change it to false or delete the script tag in the code. But the answer helped me anyway
    – Eric Fan
    Nov 21, 2012 at 4:43
  • @StevenRyssaert the last parameter is to choose if it will generate the scrip tag or not Sep 22, 2020 at 9:01
7

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

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

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

Both RegisterStartupScript and RegisterClientScriptBlock will work.

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.

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.

3

You should use RegisterStartupScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.