Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel

Last post 01-26-2008 7:31 AM by mAristos. 2 replies.

Sort Posts:

  • Sleep [|-)] Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel

    01-26-2008, 6:02 AM
    • Loading...
    • mAristos
    • Joined on 01-26-2008, 5:40 AM
    • Posts 2

    I have this problem and please if some can help me, I place here a sample code to see, but in my real problem the web user control have a very complicate javascript.

    I have a web user control that have some javascript inside, let say for example.

    1    <script type="text/javascript" >
    2    	function RunMe()
    3    	{
    4    		alert("Client id:<%=ClientID%>");
    5    	}	
    6    </script>
    7    
    8    
    9    <a href="javascript:RunMe()">Run javascript</a>
    

    a simple call to a simple javascript, inside the web user control

    And now I have a page that try to asyncronous load this control, let say...

    1        <form id="form1" runat="server">
    2        
    3    	<asp:ScriptManager ID="ScriptManager1" runat="server"  >
    4    	</asp:ScriptManager>
    5        
    6    	<asp:UpdatePanel ID="TestAjax" runat="server"  >
    7    	<ContentTemplate>    
    8        
    9    		<asp:Panel ID="panelTest" runat="server" Visible="false">
    10   			<uc1:ControlWithJavaScript ID="ControlWithJavaScript1" runat="server" />    
    11   		</asp:Panel>
    12      
    13   	    <br />
    14   		<asp:Button ID="Button1" runat="server" Text="Show panel" onclick="Button1_Click" />
    15   	
    16   	</ContentTemplate>
    17   	</asp:UpdatePanel>
    18       </form>
    

      Here is the code that open and close the web user control

    1        protected void Button1_Click(object sender, EventArgs e)
    2        {
    3            panelTest.Visible ^= true;
    4        }
    5    
    

     

    Now when I open and close the web user control I force to ajax load it inside the page, and the control is loaded, but the javascript is not run.

    Can you help me and tell me how to solve this problem ?

    Thank you in advanced. 

    Aristos

    ps - I have done my research on Internet and MSDN befoure I post it here, and I have found a solution, that is not what I search for, and not working under my real code.

    The solution is to use this code on the web user control, but
    is still missing a lot of thinks, like separate the javascript from the code
    and render them seperate.


    1    protected override void Render(HtmlTextWriter writer)
    2        {
    3            ScriptManager sm = ScriptManager.GetCurrent(Page);
    4   
    5            if (sm.IsInAsyncPostBack)
    6            {
    7                StringBuilder sb = new StringBuilder();
    8   
    9                base.Render(new HtmlTextWriter(new StringWriter(sb)));
    10  
    11               string script = sb.ToString();
    12           
    13            // need work here, to seperate the javascript from the html script
    14            // but what about a complex code that have javascript, html code, then again javascript
    15            //  and javascript inside html links etc....

    16               ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, false);
    17           }
    18           else
    19           {
    20               base.Render(writer);
    21           }
    22       }
    23  

  • Re: Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel

    01-26-2008, 6:58 AM
    Answer
    • Loading...
    • valenumr
    • Joined on 12-31-2006, 1:27 PM
    • Posts 372

    This is because normally, javascripts are not placed within the <HTML></HTML> Tags, and everything rendered in an update panel is, more or less, so the browser will see it as junk, and generate a script error (i.e. definition for RunMe not found). The best approach is as you said, to use the ScriptManager, and I find using a seperate .js file is a little easier, and you can call RegisterClientScriptInclude. I think you have to do it on PreRender though, not on Render.

  • Re: Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel

    01-26-2008, 7:31 AM
    • Loading...
    • mAristos
    • Joined on 01-26-2008, 5:40 AM
    • Posts 2

    To place the javascript out side alone is not an option because the javascript is "generated" the moment I show the button.

    Is a complicate control that I have create to select images, and manipulated on fly with out any post until the final "OK".

    I use the Yahoo Ajax User Interface to open an inside window and select an image from local folder, then I use prototype and some javascript code to manipulate the image on the flight with out any post back, like change size, place watermark, cut it, rotate it, etc...

    All that I do with javascript with a complicated code that have many variablems that I create on fly. Also this control can be used on the same page many times, and the javascript and some divs ids not hit the one the other.

    If I load the code using prototype ajax, the javascript is loaded normal, but under asp.net ms ajax style, the loader not render the javscript and I am wondering if there is an easy think to do and tell it to load it as javascript.

    By the way, if I remove the Ajax tags, all works ok, the control working good, and the aboce code work good if some one remove the ajax tags.

     

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter