473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to attach javacript to BODY tag via codebehind

I have a page that I need to attach a javascript to the BODY tag when the
page first loads (but not on postback).

Is there a way to do that via codebehind?

I've attached javascript to elements before like this:

Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();")

But the problem is that there doesn't seem to be a
System.Web.UI.HtmlControls declaration for the BODY tag.

I can actually get around the problem with a few hacks, but it'll add some
muddy HTML to the page and I'd prefer to avoid that.

-Darrel
Nov 19 '05 #1
2 2835
Use the HtmlGenericControl...
<body id="body" runat="server">

protected HtmlGenericControl body;

body.Attributes.Add(..., ...);

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <no*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP14.phx.gbl...
I have a page that I need to attach a javascript to the BODY tag when the
page first loads (but not on postback).

Is there a way to do that via codebehind?

I've attached javascript to elements before like this:

Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();")

But the problem is that there doesn't seem to be a
System.Web.UI.HtmlControls declaration for the BODY tag.

I can actually get around the problem with a few hacks, but it'll add some
muddy HTML to the page and I'd prefer to avoid that.

-Darrel

Nov 19 '05 #2
If you mean that you want the script to run when the page first loads, but
not on postback, look at RegisterStartupScript.

In your page load event:
If Not IsPostback then
Dim strScript as String = "<script language='javascript'> " _
& "Your script goes here</script>"
Me.RegisterStartupScript("NameForScript", strScript)
End If

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"darrel" <no*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP14.phx.gbl...
I have a page that I need to attach a javascript to the BODY tag when the
page first loads (but not on postback).

Is there a way to do that via codebehind?

I've attached javascript to elements before like this:

Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();")

But the problem is that there doesn't seem to be a
System.Web.UI.HtmlControls declaration for the BODY tag.

I can actually get around the problem with a few hacks, but it'll add some
muddy HTML to the page and I'd prefer to avoid that.

-Darrel

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: tabonni | last post by:
Hi I try to grab the checked files from HTML page and then send those PDF files as attachments. It can just send email, there are no PDF files attached. Can anybody point out my error? My...
17
by: Paul | last post by:
HI! I am trying to dynamically add content into Div tags with the use of JavaScript. it loads the page but adds a few characters. the script is below. <script language="JavaScript">...
4
by: bhatiaajay | last post by:
I am new to JavaScript objects. The object creates a button with no events attached. Later I want to attach an event to the button created by this object. The event attaches a function in the...
1
by: vijaygparikh | last post by:
HI i am writting a code for open default mail client and it works fine. i want to attach a file with the default mail client but file is not attached with "mailto:" option. If any one has idea...
2
by: Big D | last post by:
Is there any way to, from the page class, send literal output to the <HEAD> of te page, or within the <BODY onload="('theFunctionIAddedToTheHead');"> ? I need to do this in a class that inherrits...
3
by: Alan No Spam | last post by:
I am using Visual Studio 2003 and in order not to repeat body tag details in every page <body leftmargin=0 topmargin=0 .... I put those details as an variable in my class: public class...
2
by: weiwei | last post by:
<% Option Explicit %> <!--#include file="includes/conn.inc"--> <% Dim rds, ID %> <% Set rds = Server.CreateObject("ADODB.Recordset") %> <% rds.Open "select RecID, LocationName from Location ORDER...
4
by: weiwei | last post by:
<% Option Explicit %> <!--#include file="includes/conn.inc"--> <% Dim rds, ID %> <% Set rds = Server.CreateObject("ADODB.Recordset") %> <% rds.Open "select RecID, LocationName from Location ORDER...
3
by: juergen.riemer | last post by:
Hi all, I have a problem in attaching an external stylesheet to a newly created iframe in Firefox (1.5.0.2). If I use the code below (with a relative address to the file) the stylesheet would...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.