ASP.Net - Script Function Question

Posted on 2006-04-24
Last Modified: 2012-06-27
Hi all.  I have the following setfocus script that runs perfect but I have one hitch.  There is a control file in my application that already has an window.onload event.  So what is happening is that onload is getting overwritten by the unload call in this function.  How can I get this function to load and not screw up my other function somehow any idea?  I cant mess with that other onload function because it is menu specific.  So I need to somehow get this code to fire as well.  I call this setfocus in my itemdatabound and it works it just causes issue to my menu onload.  As you can see there is a line calling the onload here outside the function brackets.

            protected static void SetFocus(Control control)
                  StringBuilder sb = new StringBuilder();
                  sb.Append("\r\n<script language='JavaScript'>\r\n");
                  sb.Append("function SetFocus()\r\n");
                  Control p = control.Parent;
                  while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
                  sb.Append("window.onload = SetFocus;\r\n");
                  sb.Append("// -->\r\n");
                  control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
Question by:sbornstein2
    LVL 15

    Accepted Solution

    Instead of RegisterClientScriptBlock, did you try using RegisterStartupScript.

    Also the key registered can be checked for the uniqueness.
    LVL 10

    Expert Comment


    Compare your code with given sample in following Link


    Author Comment

    SystemExpert what am I am comparing this with on that stuff?

    Author Comment

    deepaknet just tried that and no dice same problem

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now