.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Where to place JavaScript in ASP.NET, master page?

Posted By:      Posted Date: October 27, 2010    Points: 0   Category :ASP.Net

I have content pages that inherit from master page, in content page I tried adding JavaScript event for both asp.net button and regular html <input type="button"> and the don't fire the script to which I linked master page:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:Button ID="Button1" runat="server" onclientclick="MsgBox()"
    Text="Button" />
    <input id="Submit1" type="button" value="print" onclick="printPage()" />

here is master page link in the <head></head> tag to js file:

<Script href="~/Scripts/JScript.js" type="text/javascript" />

simple javascript for testing:

function printPage() {

function MsgBox() {

And what is a better alternative doing it like this or using ClientScripManager: http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx - Also did not work for me. I think I missing something...

View Complete Post

More Related Resource Links

Re-positioning an HTML container with javascript onresize with a Master page.


I have an HTML <div> container that I float to the right of a gridview.  When the browser window is maximized, it looks fine.  When minimized, the gridview positions below the floating container and most of the data is pushed off the bottom of the browser - you have to scroll down to see it.

If I position the floating container above the gridview it looks fine in a minimized window, but looks real bad when maximized.

What I want to do is use a javascript: onresize event to re-position the floating container up or down depending on the window.inner.width AND do it with a Master/Content page structure AND do it for only the one page where needed.

I can capture the onresize event and display the window dimensions but haven't figured-out how to do it with a Content page that still uses the Master page.  I think I'm having a very "thick" week...

- Tinker


collapsiblepanelextender, master page and javascript

does anyone have any working code that uses javascript to open and close a cpe when the CPE is in a masterpage?  It seems like the javascript never is able to find the CPE when it is on a master page.  I have an DropDownList on the page and when certain items are selected, i want to collapse or expand when others are selected.  It seems to work fine on a regular page, but not when it is a page nested in a master page.  It is adding a suffix to the CPE and javascript is unable to find it.  any help would be much appreciated!<script type="text/javascript" language="javascript">        function ExpandCollapse() {            var e = document.getElementById("<%=DropDownList.ClientID%>")            var strUser = e.options[e.selectedIndex].value;            var collPanel = document.getElementById("<%=CPE.ClientID%>")            if (collPanel = null)                alert('No panel found');            if (strUser = "Collapse")    

How to call javascript function in master page

helloi have javascript file 'script.js' where i have writt en a function for print. but this function is working fine if i am not using master page in my report page but the question is i want to use master page for my report page where i am calling javascript function but it is not working . Do help me regarding MasterPage and Javascript function.

Image background color is showing while i drag in content place holder in master page ???


Hi  ...

I had few icons and images in my old web project which had no issue with the images and icons background color.

Now i have changed the total look and layout of my web site. After doing so , my old images and icons as well as new images and icons backgroud color is also displaying in the image.

The same image is looking good in my old web site layout ...

I tried all the stuff to show only image not image background ... But , it is not happening ....

The main thing is the same images and icons background color is not showing in my old design ...

Look at the image background , it has grey color background. It was not happening in my old web site design

Plz help me to solve this .... 

How to include javascript reference in master page Asp .Net C# 2.0


I am using Asp.Net C# 2.0. My website is working fine in local. Website contains 2 js files included in master page.  It works fine in local environment, but when i publish my website i get "Object expected" error on page load, and thus the js functions are not working in published website. 

Currently i am writing <script src="/javascripts/jquery.hotkeys-0.7.9.js" type="text/javascript"></script> in master page.

Looking forward for help.

Thanxs in Advance.

Unique content within master page based on conditions met


 Hi all-

I would like to present users with unique content based on certain conditions being met as they land on the home page (default.aspx).  The condition logic will be in the VB code behind Page Load event. The content will go in a content placeholder as specificed from the master page. 

However, the content change wouldn't be something small (ie making a panel or label visible or not) but rather it will be different HTML,  Divs and databound gridview content.  The HTML content will be stored in the database.

Can someone point me in the right direction for best practices on how to accomplish this?


Passing Linq Statement between master and content page


I usually work with windows forms, and am just starting to work with ASP.Net, so appologies if this is a dumb question!

I have a master page which has a search button on it. The results of this are built up using a Linq statement and if there are any results, I want to display the results in a contents page.

On the masterpage, I have the following code:

Private _res

    Public Overridable ReadOnly Property SearchRes()
            Return _res
        End Get
    End Property

 Private Sub DoSearch()
   'some code to get a linq result

    If res.Any Then
            _res = res
            Server.Transfer("SearchResults.aspx", True)
            'no results
            LabelNoRes.Visible = True
        End If
 End Sub

On the SearchResults.aspx page I have this code.

Dim m As pxSite = CType(Page.Master, MasterPage)
        If m.SearchRes IsNot Nothing Then
            Label1.Text = "Results found = " & m.SearchRes.Count
        End If

m.searchRes is always nothing. 

What am I doing wrong here?


Content page control passes as argument to javascript function


Here is the content page:


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="JavaDynamicCopyValue.aspx.cs" Inherits="JavaDynamicCopyValue" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <script type="text/javascript" >
 function copyValue(input, output )
   ouput.value = input.value;
   return true;
 <asp:TextBox ID="txt1" runat="server" onchange="copyValue(this,document.getElementById('<%=txt2.ClientID%>'))"></asp:TextBox>
    <br />
    <br />
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>


I got a javascript run time error says output is undefined. if I view source of the page, the txt1 markup became:

<input name="ctl00$ContentPlaceHol

Not getting multiple composite controls on master page


I have created two composite controls in the same assembly and registered that assembly in a master page using <%@ Register tag.

When I try to use these controls using the registered tag prefix I am getting only one control along with the tagprefix .

How to get all the composite controls present in the registered assembly?

Master Pages: Master Your Site Design with Visual Inheritance and Page Templates


Master pages in ASP.NET 2.0 solve a problem many Web developers have been solving on their own with a variety of techniques for years - providing a single master template for an entire site. This article covers the details of master pages, discussing their usage and implementation and how they are a natural evolution of custom techniques developers are using today.

Fritz Onion

MSDN Magazine June 2004

ASP.NET Proxy Page - Used for Cross Domain Requests from AJAX and JavaScript

The article ASP.NET Proxy Page - Used for Cross Domain Requests from AJAX and JavaScript was added by Aamir Hasan on Thursday, August 19, 2010.

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn't allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull content

Dynamic Checkboxlist exception when adding to Panel when using Master Page


In essence, I am creating Dynamic Checkboxlist controls and adding them to a Panel.  It works well when I do not have a Master Page configure to this webpage. Once I have a masterpage congifured.. I get a "Object Reference not set to instance of object" error on the Panel_Control.Controls.Add(Checkboxlistnew) line:

protected void Page_PreInit(object sender, EventArgs e)
        XML OXML = new XML();
        alistChkBoxLabels = OXML.XMLTransform("C:\\Documents and Settings\..\\Config_new.xml", "config_options/check_box/applications/app/@name");
        int count = 1;
        alistCheckisFacility = new ArrayList();
        alIDTracker = new ArrayList();
        while (count <= alistChkBoxLabels.Count)

            Checkboxlistnew = new CheckBoxList();
            Checkboxlistnew.ID = count.ToString();
            Checkboxlistnew.Items.Add(new ListItem(alistChkBoxLabels[count - 1].ToString(), alistChkBoxLabels[count - 1].ToString()));

            String sAppName = alistChkBoxLabels[count - 1].ToString().Trim();

            alistCheckisFacility = OXML.XMLTransform("C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2005\\WebSites\\User_Trial\\Config_new

Button click in master page use in other pages I have to click twice. Why?


In my masterpage I have a menu with buttons. I want the buttons to do something after click. It works but I have to click twice on the button, why? What do I have to do to just to click once?

Is master page a best alternative for Frame Sets ??


My webpage has one banner with a logo, a vertical menu bar on the left side of the page and a footer . I have 5 other pages which should get loaded when the user select them from the menu bar. what my concern is actually the banner,  menu bar  and the footer should not change when the requested page loads. This definitely can be handled by sets. But I want to know whether there are any other alternatives for frame sets or could master page be helpful for this purpose ????? 

Any help would be appreciative .

DropDownMenu disapper in master page


Dear all,


I have master page and wen i use master page my drop down menu selection disapper in web forms...could anyone pls help me...am not so clear about z-index...








@ Master Language="VB" AutoEventWireup="false" CodeBehind="Default.master.vb" Inherits="E_Management._Default1" %>

Apply custom.master to "Site Settings" page?


I'm using a custom.master page, which is applied to almost all pages available to READ users.

However, I'd like to apply it to the Search, Site Settings, and View All Site Content pages.

I'm not sure where this would be applied.

Guidance much appreciated. Thanks!

Sharepoint 2007 - Wiki site - New Pages Not Inheriting Custom Master Page


I am working on a SP site (MOSS not WSS) which is heavily customised away from the OOTB layout. I need to create a Wiki site, which was going well - I've created a master page which displays the miniconsole (this was not visible with our customised layout), and made it the default master page for the site. This has been applied to the two standard pages that come with the Wiki site, however when a new page is created, it seems to inherit the default.master, rather than my custom master page - thus not showing the miniconsole.

When I have created a new page, I am able to use SharePoint Designer to attach my custom master page to it, which works perfectly, however this will not be a viable option going forward as end-users create their new wiki pages.

How can I get the new pages to inherit my master page when they are created?


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend