6

I am looking for a way to insert javascript code block to end of ASP.NET page.

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "showVideo", sScript, true);

is appending to body but js codes are always requesting some js files didn't load or some functions are below of the script.

How can i append scripts that i generated dynamically to the bottom of body?

Thanks for your help.

uzay95
  • 16,052
  • 31
  • 116
  • 182
  • Does this answer your question? [Difference between RegisterStartupScript and RegisterClientScriptBlock?](https://stackoverflow.com/questions/666519/difference-between-registerstartupscript-and-registerclientscriptblock) – Mehdi Dehghani Feb 27 '20 at 05:08

2 Answers2

11

We are using something like

    public static void GenerateJsTag(this TemplateControl page, string jsCode)
    {
        var jsLink = new HtmlGenericControl {TagName = "script", InnerHtml = jsCode };
        jsLink.Attributes.Add("type", "text/javascript");
        page.Controls.Add(jsLink);
    }

hope that will help ;)

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
  • Thank you Thomas. Your solution is also very good. I found the answer now and posted but i will mark your answer. Thanks again. – uzay95 Apr 04 '10 at 10:39
11

Adding Client-Side Script Blocks with RegisterStartupScript() and RegisterClientScriptBlock()

The only difference between these two methods is where each one emits the script block. RegisterClientScriptBlock() emits the script block at the beginning of the Web Form (right after the tag), while RegisterStartupScript() emits the script block at the end of the Web Form (right before the </form> tag).

Cory House
  • 14,235
  • 13
  • 70
  • 87
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • 1
    Very important to note that RegisterScript methods are related to
    not body or html tags. @uzay95 thanks for the clarification.
    – Miguel Aug 17 '16 at 18:54
  • 1
    You'd think these methods would have more descriptive names like RegisterScriptAtBeginning() and RegisterScriptAtEnd() because I *still* can't remember which is which. But that's Microsoft I guess – shiggity Aug 16 '17 at 17:52