How ClientScriptManager Methods Work

For all the methods mentioned here, you have to pass in the first argument the client script type for which you want to register the given code. When you are working in the context of the page (Page), simply writing GetType() is enough. All methods also require the name/key of the script you want to register, so that, when used correctly, you never insert the same script more than once (for example on a PostBack). The list of methods here is not complete; I only mention the ones I understand at least reasonably well. In every method I passed only this JS – prikaz();.
1. RegisterClientScriptBlock – Puts the input you pass in the 3rd parameter into a block. If you do not specify the 4th argument or set it to false, <script type="text/javascript">// <![CDATA[ is not added to the beginning of the input and </script> is not added to the end.

2. RegisterClientScriptInclude – In one overload, this method also works without a class (Type); it then takes 2 arguments – a key and a URI. It generates the following code into the section: <script src="zadana_adresa" type="text/javascript"> </script>
3. RegisterHiddenField – the C# code Page.ClientScript.RegisterHiddenField("hfSecret", "pa$$word"); generates the following: <input id="hfSecret" name="hfSecret" type="hidden" value="pa$$word">– into the section, of course.
4. RegisterOnSubmitStatement – It has 3 parameters – type, key and JavaScript. It generates a method similar to the following into the section, although a little lower than the previous methods:
JS
<script type="text/javascript">
//<![CDATA[
        function WebForm_OnSubmit() {
            prikaz();
            return true;
        }
        //

 

The only thing I passed to the method was the text prikaz();, everything else is generated automatically. For this method, the third parameter (the JS code) cannot be empty (String.Empty).
 
5. RegisterStartupScript – If you do not specify the 4th parameter, again only the text you passed in parameter 3 is inserted into the page. This time, however, it is inserted at the end of the page, before the end of the last <form> tag, of which there is usually only one in ASP.NET. If you pass True in the 4th parameter, you get output like this.

CODE
<script type="text/javascript">// <![CDATA[prikaz();// </script>

So as you can see, no method inserts JavaScript into the tag. You can find a possible solution from Stack Overflow at

, and I will also try to come up with something of my own. I already had one, but for completely incomprehensible reasons it encodes, for example, " or ' into ' and other entities.

 

EDIT:

I have now found a way to insert a script into the head; here it is only JavaScript, but adapting it is easy:

C#
internal static void InjectInternalScript(Page page, string javaScript)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<script type=\"text/javascript\">");
    sb.Append(javaScript);
    sb.Append("</script>");
    LiteralControl literalControl = new LiteralControl(sb.ToString());
    page.Header.Controls.Add(literalControl);

}

Categories

Leave a Reply