Skip to main content

How to Programmatically add JavaScript File to Asp.Net page?

In this post I will show you how to add JavaScript files programmatically  to your asp.net page. 

Just add this code to the Page_Init event handler on your page:




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Blog_Tips.aspx.cs" Inherits="Blog_Tips" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        How to Programmatically add JavaScript File to Asp.Net page?
    
    </div>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Blog_Tips : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {
        HtmlGenericControl javascript = new HtmlGenericControl("script");
        javascript.Attributes["type"] = "text/javascript";
        javascript.Attributes["src"] = "Scripts/jquery-1.3.2.min.js";
        Page.Header.Controls.Add(javascript);
    }
}

Comments

  1. thank you for your topics ..
    that is good for all asp developer ..

    ReplyDelete

Post a Comment

Popular posts from this blog