How a javascript function be called at onload from with in the asp:content

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SterexID
    New Member
    • Mar 2010
    • 2

    How a javascript function be called at onload from with in the asp:content

    Hi,

    i want to call a javascript method when the page loads (aspx). i.e. using onload() function......

    I am using the appication.mast er page of the MOSS 2007 as the master page for my application...s ince there is no body tag in the asp content page how should i call the javascript method that i have put in the
    Code:
    <asp:Content ID="Content3" ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">function OnLoadFunction()
    {
    document.getElementById('TxtTab').style.display='none';
    SwitchTabs();
    }
    				
    function SwitchTabs()
    {var tb = document.getElementById('TxtTab').value;
    for (var i=1; i<3; i++)
         {
       document.getElementById("Buttion"+i).style.cssText=	"BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 0px solid; BORDER-BOTTOM: black 1px solid; background-color: Gainsboro;" 
         document.getElementById("tblPage"+i).style.display ="none";
       }
         document.getElementById("Buttion"+tb).style.cssText="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 0px solid; BORDER-BOTTOM: black 0px solid; background-color:  white;" 
    
    document.getElementById("tblPage"+tb).style.display='';
    					
    
       }
    
    function SelectTab(v)
    {
    					
    document.getElementById('TxtTab').value=v;
    SwitchTab(); 
    return false;
    } </asp:content>
    how do i call the OnLoadfunction method onload ?

    Can some body help me in this regard....
    Last edited by Frinavale; Mar 29 '10, 01:54 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    Hi,

    You should be able to call it in the normal manner. You do not have a body tag in your content page but it will be in what is delivered to the client. Since JavaScript is executed on the client it will be able to access the body tag. You would be able to execute a function when the onload event is triggered using:
    Code:
    window.onload = OnLoadFunction;
    I assume that your placeholder is surrounded by script tags. Also please note that this method of handling events is only as an indicator - using event handlers is much better and is recommended.

    P.S. Please use code tags when supplying code as it improved readability

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Don't worry about the body tag stuff.
      Your body tag is in your Master Page.

      When your page is compiled, it is compiled "With" the Master Page in order to create the HTML output for the page.


      The problem you are having is that your controls that you are trying to retrieve in your JavaScript using the document.getEle mentById() method is not working...(righ t?)


      It's because when you are using a Master Page, the controls (eg: text box, button etc) in your content page are given a unique client ID. The client ID generated usually something like "ctl00_contentP laceHolderID_Co ntrolID".

      Say you have a TextBox in your ASPX page, and you have specified that the ID for the TextBox is "TextBox1". When you are working with this TextBox on the server you can refer to it as "TextBox1". But, when this TextBox is rendered as HTML it is given another ID so that it is unique in the page (you can have another TextBox1 in another control on the page). The ID for the HTML element that represents the TextBox will become "ctl00_PlaceHol derAdditionalPa geHead_TextBox1 ".

      What you need to do in your JavaScript is grab the ID of the HTML element that represents the TextBox.

      To do this you need to use the ClientID Property of the TextBox control. The ClientID is a property of the ASP.NET TextBox control....you can use this when you are generating your JavaScript by writing the ClientID of the TextBox control into your JavaScript.


      Ok let's step back for a second....way back.

      In ASP (classic...and ASP.NET) anything between <% %> is executed on the server.

      So if you have:
      VB Code:
      Code:
      <% Response.Write("hello world") %>
      Response.Write is executed on the server. It is executed on the server and will write "hello world" into the page where the code exists in the page.

      If you were using C# you would need to have C# code in between the <% %>:
      Code:
      <%Response.Write("hello world"); %>
      In ASP (classic) the short hand for Response.Write is <%= %>.

      So you can have (VB):
      Code:
      <%="hello world" %>

      Ok, now that the basics have been covered let's get back to the code.
      If you want to write the ClientID of a control into your JavaScript, you could have the following:
      Code:
      <script type="text/javascript">
      function displayTextInTextbox(){
        var textbox1 = document.getElementById('<%=TextBox1.ClientID%>');
        var text = textbox1.value;
        alert(text);
      }
      Notice how I used <%= TextBox1.Client ID %> to write the client ID of the TextBox1 control into the JavaScript.

      It's pretty simple.

      You will have to do this for all of your ASP.NET controls that you need to access in your JavaScript.


      Happy coding!

      -Frinny


      </script>

      Comment

      • SterexID
        New Member
        • Mar 2010
        • 2

        #4
        How do i call the javascript method OnLoadFun() during onload of the aspx page ?
        I am using window.onload inside the placeholdermain asp:content embedded within script tags.....but it is giving me unknown error ...i am also using the clientID property to access the id of the controls....

        As i said i am using the application.mas ter of sharepoint server 2007 as the master page and i want to call the OnloadFun method during loading of the content page....i have placed the javascript method within the PlaceHolderAddi tionalPageHead tag....

        Thanks very much for your previous reply in detail

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Oh my, how did I miss that you were working on a sharepoint server.
          I have never worked with sharepoint before but I have heard that the more you want to do, the hard it becomes to use

          In this case I think it's just where you are placing the JavaScript.


          The method that you want to execute during the window.onload event has to be in the <head> section of the page (for some reason). The window.onload JavaScript code can be within the <body> section of the page.

          I have moved this question to the JavaScript forum because I think you'll get more help (my forte is asp.net)

          :)

          -Frinny

          Comment

          Working...