How to call javascript on usercontrol's load event

Last post 09-17-2009 7:17 AM by soyka. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 11-07-2008 6:51 AM

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    How to call javascript on usercontrol's load event

    Hi all,
    i am developing one project in which i do have only one Default.aspx
    page and all other are the "UserControls" which are going to be loaded run time
    depending upon the database setting.
    and in one usecontrol i have to execute one javascrip when that usercontrol
    is going to be loaded
    My problem is how to do that follwoing option not working

    1. Calling javascript on <Body onload="MyFun()"> since there is no <body> element
       allowed in that
    2. this.page.RegisterStartUp("nameofthewindow","<script    language='javascript'>alert("function here");</script>"); // is not woring
    3 Page.ClientScript.RegisterStartupScript(this.GetType(),
      "ClientCall", "<script> function()
       {}</script>");

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  •  Advertisement

    Featured Advertisement

     
  • 11-07-2008 7:44 AM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Slacker
    • Points 19,057

    Re: How to call javascript on usercontrol's load event

    This is what I do in my pages

    //Include an external Javascript
     if (!Page.ClientScript.IsClientScriptIncludeRegistered("displayRow"))
                    Page.ClientScript.RegisterClientScriptInclude("displayRow", ResolveClientUrl("showhide.js"));

          
    //call the function
         string temp = "selectiveDisplayS3(" + stype + ")";

    //find the body tag
                HtmlGenericControl body = (HtmlGenericControl)Page.FindControl("Body");
    //if your body is in master page 

                HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("Body");

               body.Attributes.Add("onload", temp);

    To get this to work, you need set this

    <body runat="server">

  • 11-08-2008 1:42 AM In reply to

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    Re: How to call javascript on usercontrol's load event

    Hi XXd,

    I did it following way....

    <scirpt  type="text/javascript" language="javascript">

    window.onload=function{

    // my javascript goes here

     

    }

    <script>

    and this worsk fine.

    but still i want to understand your code i am not getting what is  "selectiveDisplay("+stype+")"

    and "stype" stands for

    i would thankful to you if you give me time to make me understand this

     

     

     

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  • 11-09-2008 5:50 AM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Slacker
    • Points 19,057

    Re: How to call javascript on usercontrol's load event

     sorry, I copied and pasted hastily a piece of code I wrote some time ago. Stype is just one variable name. Sorry for confusing you. 

  • 11-10-2008 12:48 AM In reply to

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    Re: How to call javascript on usercontrol's load event

    Thanks for your time

     

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  • 04-02-2009 9:10 AM In reply to

    • siiniqi
    • Top 500 Contributor
    • Joined on 10-20-2007
    • Wannabe Slacker
    • Points 5

    Re: How to call javascript on usercontrol's load event

    in page load

               HtmlGenericControl lightboxJs1 = new HtmlGenericControl();
                lightboxJs1.TagName = "script";
                lightboxJs1.Attributes.Add("type", "text/javascript");
                lightboxJs1.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
                String strprototype;
                strprototype = ConfigurationManager.AppSettings["lightbox_album_prototype"];
                lightboxJs1.Attributes.Add("src", ResolveUrl(strprototype));
                this.Page.Header.Controls.Add(lightboxJs1);

     

     

    add javascript in app_settings

    <add key="lightbox_album_prototype" value="J_Tools/lib/prototype.js"/>

    Filed under:
  • 06-02-2009 2:04 AM In reply to

    • kshewani
    • Not Ranked
    • Joined on 06-02-2009
    • Wannabe Slacker
    • Points 5

    Re: How to call javascript on usercontrol's load event

    ConfigurePortfolio1.Page.RegisterClientScriptBlock("onload", "<script language='javascript'>alert('Control loaded');</script>");

    Note: "ConfigurePortfolio1" is a usercontrol.

     

  • 09-17-2009 7:17 AM In reply to

    • soyka
    • Not Ranked
    • Joined on 09-17-2009
    • Wannabe Slacker
    • Points 5

    Re: How to call javascript on usercontrol's load event

     Thanx - your code was a great lead! I had to make a few minor changes though

    1) in my master page I set the ID tag as well

    <

    body id="mybody" runat

    ="server">

    2) in the userControl's Page_Load eventhandler

     

     

     

     

     

     

     

     

    If

    Not Page.ClientScript.IsClientScriptIncludeRegistered("clock")

    Then

    Page.ClientScript.RegisterClientScriptInclude(

    "clock", ResolveClientUrl("~/JavaScripts/ClockFunc.js"))

     

     

    End If

     

     

     

     

    Dim body As HtmlGenericControl = CType(Page.Master.FindControl("mybody"), HtmlGenericControl)

    body.Attributes.Add(

    "onload", "startTime()")

     

Page 1 of 1 (8 items)