Page view counter

Javascript in a UpdatePanel?

Last post 07-02-2007 7:11 AM by Jonathan Shen – MSFT. 8 replies.

Sort Posts:

  • Javascript in a UpdatePanel?

    06-27-2007, 11:55 AM
    • Loading...
    • offwhite
    • Joined on 09-19-2005, 5:08 PM
    • Milwaukee, WI
    • Posts 206
    • Points 959

    I have a TabStrip and a MultiView which allows the user to move between two views. The views simply hold onto user controls. Both of these user controls use the UpdatePanel. The parent container holding the MultiView also wraps everything with an UpdatePanel to smooth the transition from one view to another.

    Here is my problem. I am using a static Javascript source file that holds all of the custom functions both of the user controls use. But they need to have some variables defined which are set within the user controls. But when the page is first loaded it only shows the first view in the MultiView. As a result it only emits the Javascript for that user control. When the second view is shown it does not get the Javascript it needs which is a part of the user control.

    What I would like to know is how to emit Javascript through an UpdatePanel. There has to be a way to get my data across. This bit of code is inline Javascript that takes values like TextBox1.ClientID which is set to a Javascript variable. That variable is used in the static source file. The static source file makes it much easier to debug. But I am trying to ensure the user controls work independently as well as within the MultiView and UpdatePanel structure.

    What can I do to make sure the Javascript variables are set for the second view? 

    Brennan Stehling
    http://www.smallsharptools.com/
    http://www.linkmindr.com/
  • Re: Javascript in a UpdatePanel?

    06-27-2007, 3:29 PM
    • Loading...
    • paul.vencill
    • Joined on 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 1,062
    • Points 4,802

    One option is to not use inline javascript at all (a best practice anyway...).

    In the onLoaded event of the pagerequestmanager you can set up a function to run that will set up your variables for you, e.g.:

    var inputs = document.getElementsByTagName('INPUT");

    for (var i=0,i<inputs.length;i++){
         if(inputs[i].type == 'TEXT')
               evaluateInput(inputs[i]);
    }

    . . .

    function evaluateInput(input){
           // perform logic to map your input to whatever variable you need them to map to.
    }

    There's a dozen ways to skin this cat, lmk what you're trying to do. 

    Help those who have helped you... remember to "Mark as Answered"
  • Re: Javascript in a UpdatePanel?

    06-27-2007, 3:45 PM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 970
    • Points 4,578

    Hi,

    Have you tried one of the RegisterXXXScriptYYY method of the  ScriptManager Class?

    RegisterClientScriptBlock(Control, Type, String, String, Boolean) maybe?

  • Re: Javascript in a UpdatePanel?

    06-27-2007, 4:12 PM
    • Loading...
    • KaziManzurRashid
    • Joined on 03-09-2003, 3:04 PM
    • Dhaka, Bangladesh
    • Posts 882
    • Points 4,782

    You should use the ScriptManager various methods for complex script injecting. Alternatively  for simple Control ID injection i think the following is more compact:

    var aButton = $get('<% = btnA.ClientID %>');

    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
  • Re: Javascript in a UpdatePanel?

    06-27-2007, 7:11 PM
    • Loading...
    • offwhite
    • Joined on 09-19-2005, 5:08 PM
    • Milwaukee, WI
    • Posts 206
    • Points 959

    I appreciate these responses but they just are not sufficient for the problem. I cannot get any Javascript from the second view to affect the page when it is activated within the UpdatePanel. You cannot register a script either. I have been doing that for the user controls for regular behavior and want a way to make it work within the UpdatePanel. 

    And for placing data into an input field also presents a catch-22 scenario. I need to know the ID of the input field in order to get to it so that I can interpret the data. The way the UpdatePanel works there is really no way to determine which control is active or if I later use a databound control which may include many input fields. It does not seem there is a solid way to this in a standard ASP.NET way.

    What I think I will have to do is get more aggressive with custom Javascript and create my own mechanisms to know what view is active and what is being updated and when. I just do not think there is a way to make it as fluid as it can be with the server-side code which is able to deal with the hierarchy of the controls. It seems that the UpdatePanel does not have any way of addressing nested controls. 

     

    Brennan Stehling
    http://www.smallsharptools.com/
    http://www.linkmindr.com/
  • Re: Javascript in a UpdatePanel?

    06-28-2007, 3:59 AM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 970
    • Points 4,578

     For this problem maybe I would start with the followings:

    - if only one usercontrol visible/used at the same time,  with  RegisterStartupScript(Control, Type, String, String, Boolean) set the shared control specific js variables when the control loaded, maybe that enough.

    - if more than one usercontrol visible at the same time, convert the shared js variables to arrays, populate/expand the array with the control specific data when the control loaded. Of course modify all the shared js functions to detect automatically from which usercontrol are called (travelling the dom tree etc.)

    - both solution should work with or without updatepanels
     

    What do you think?

     

  • Re: Javascript in a UpdatePanel?

    06-28-2007, 5:44 PM
    • Loading...
    • offwhite
    • Joined on 09-19-2005, 5:08 PM
    • Milwaukee, WI
    • Posts 206
    • Points 959

    I am not sure that will work but I can put together a test website tonight to try it out. I will post the page here. 

    Brennan Stehling
    http://www.smallsharptools.com/
    http://www.linkmindr.com/
  • Re: Javascript in a UpdatePanel?

    06-30-2007, 4:28 PM
    Answer
    • Loading...
    • offwhite
    • Joined on 09-19-2005, 5:08 PM
    • Milwaukee, WI
    • Posts 206
    • Points 959

    I was able to create a working sample project using the RegisterScriptBlock method in the Page_Load. I have posted the sample project with a zip download here...

    http://www.offwhite.net/AjaxSample/Default.aspx 

     

    Brennan Stehling
    http://www.smallsharptools.com/
    http://www.linkmindr.com/
  • Re: Javascript in a UpdatePanel?

    07-02-2007, 7:11 AM

    hi offwhite, 

    Thanks  for your solution!

    Jonathan Shen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (9 items)