Masterpages and clientscript?

H

HockeyFan

I've done this many times with average pages, but in a case I have
now, I have a master page with some controls on it. Under certain
conditions when the user selects certain options, I want to execute a
javascript function on the masterpage. Since I make this
determination in the codebehind, I was hoping that I could call
ClientScript to set up the call to the function. However,
CLientScript doesn't appear to work in masterpages. Is there a way in
the codebehind of a masterpage, to programmatically initiate certain
javascript in the masterpage clientside (ala ClientScript)?
 
T

Teemu Keiski

Hi,

can you clarify how it doesn't work? Here's a very simple example I tried
with:

MasterPage.master
=============

<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

MasterPage.master.cs
===============

//imports excluded

public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(MasterPage), "hello",
"alert('Hello from master page');", true);
}
}

content.aspx
=========

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="content.aspx.cs" Inherits="content"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:Content>

content.aspx.cs
===========

//imports excluded

public partial class content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(content), "hello",
"alert('Hello from content page');", true);
}
}


You'll see that when running content.aspx, it first alerts you "Hello from
content page" and then "Hello from master page" which demonstrates that both
calls work. So can you clarify how you call Page.ClientScript in your case?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top