How to add java script to asp:Content

Last post 07-06-2009 1:29 AM by demoninside9. 10 replies.

Sort Posts:

  • How to add java script to asp:Content

    07-04-2009, 2:44 AM

    HI friends,

                  in a simple .aspx form see.. this

    <script language="javascript" src="wysiwyg.js"></script>
    <script type="text/javascript">

        function SelectAll(id) {//somthing....
                }
         

    </script>

    <body onload="generate_wysiwyg('txtMessage');">


    but now i want to use  a master page with this.

    now please let me know how to add this all in

    <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server"></asp:Content>

    there is no body in this <asp:Content>

    please help me out

    thanx

    never let you down..always be happy...
  • Re: How to add java script to asp:Content

    07-04-2009, 3:11 AM

    give id tag to body tag like id = "pagebody" in the mastepage and give the attribute runat="server", then

    in your content page,write like this

    HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("pagebody");
    body.Attributes.Add("onload", "generate_wysiwyg('parameters');");

    now keep your javascript function, in between Content tag.

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to add java script to asp:Content

    07-04-2009, 3:15 AM
    • Member
      270 point Member
    • veenag81
    • Member since 07-04-2009, 7:04 AM
    • Gandhinagar,India
    • Posts 61

    Hi demoninside9, u can add your script like this ---->

    <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server">

    <script type="text/javascript" language="JavaScript">

    /// in this u add your java script code

    </script>

    </asp:Content>

  • Re: How to add java script to asp:Content

    07-04-2009, 4:04 AM

    thanxfor replying

    I have give in masterpage' body tag

    <body id="pagebody" runat="server">

    and in

    <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server" >
    <script type="text/javascript">

        function SelectAll(id) {//..............

        }
         HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("pagebody");  
     body.Attributes.Add("onload", "generate_wysiwyg('txtMessage');");

     <link href="styles/styles.css" rel="stylesheet" type="text/css" />

    </script>

    </asp:Content>

    but still not working...

    thanx

    never let you down..always be happy...
  • Re: How to add java script to asp:Content

    07-04-2009, 4:20 AM

    in body onload attributes, your added function name is "generate_wysiwyg", but in javascript your function name is selectAll, make the function names as same.


    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to add java script to asp:Content

    07-04-2009, 4:23 AM

    those both are different

    SelectAll is  for other purpose and generate_wysiwyg is a JS file which I am calling on body load

    thanx

    never let you down..always be happy...
  • Re: How to add java script to asp:Content

    07-04-2009, 4:34 AM
    • Member
      270 point Member
    • veenag81
    • Member since 07-04-2009, 7:04 AM
    • Gandhinagar,India
    • Posts 61

    Hi demoninside9, u can try for this ---->

    <head id="Head1" runat="server">

    <script language="javascript" src="wysiwyg.js">

    function SelectAll(id) {//somthing....
                }
         

    </script>

    <body onload="generate_wysiwyg('txtMessage');">

    </head>

     


  • Re: How to add java script to asp:Content

    07-04-2009, 4:34 AM
    • Member
      270 point Member
    • veenag81
    • Member since 07-04-2009, 7:04 AM
    • Gandhinagar,India
    • Posts 61

    Hi demoninside9, u can try for this ---->

    <head id="Head1" runat="server">

    <script language="javascript" src="wysiwyg.js">

    function SelectAll(id) {//somthing....
                }
         

    </script>

    </head>

    <body onload="generate_wysiwyg('txtMessage');">

    </body>


     


  • Re: How to add java script to asp:Content

    07-04-2009, 4:38 AM

    Not working.....................

    shows error

    You can only have one <head runat="server"> control on a page.

    cos i think master page is also having head tag

    thanx

    never let you down..always be happy...
  • Re: How to add java script to asp:Content

    07-04-2009, 4:52 AM
    Answer

    do as i suggested in my prev. post, it worked for me. see this example.

    Master Page:
    <%@ Master Language="C#" AutoEventWireup="true"  %>
    <!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 id="pagebody" runat="server">
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
           </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    </html>
    Content PAge:
    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="contentpagejavascript.aspx.cs" Inherits="contentpagejavascript" Title="Untitled Page" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <script type="text/javascript">
            function CheckAlert()
            {
                alert("fired");
            }
        </script>
    </asp:Content>
    
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class contentpagejavascript : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("pagebody");
            body.Attributes.Add("onload", "CheckAlert();"); 
        }
    }
    




    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to add java script to asp:Content

    07-06-2009, 1:29 AM
    Answer

    MasterPage

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="my.master.cs" Inherits="my"  %>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head id="Head1" runat="server">
        <title></title>
        
        <link href="CSS/gauravcss.css" rel="stylesheet" type="text/css" />
    </head>
    <body id="pagebody" runat="server">
        <form id="form1" runat="server">

    ........

    </form>
    </body>
    </html>

    asp:Content code file page..

    public partial class SendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label1.Visible = false;
                
                
            }
            HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("pagebody");
            body.Attributes.Add("onload", "generate_wysiwyg('txtMessage');");

           
        }

    }

    where I have mistake...

    wysiwyg is a java script file, which i have put where all my .aspx pages are residing.(in same folder)

    thanx

    never let you down..always be happy...
Page 1 of 1 (11 items)