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