I am currently facing a challenge with my custom control, where I perform a query to retrieve records and dynamically add HTML controls to the page based on the data.
The issue arises when trying to incorporate dynamic JavaScript into this process.
To address this, I utilize literal controls.
<asp:Literal runat="server" ID="latEventToolTipJqueryScripts"></asp:Literal>
This method works seamlessly.
<script language="javascript" type="text/javascript">
// <![CDATA[
Sys.Application.add_load(WireEvents_<%=this.ID%>); // fix wiring for .NET ajax updatepanel
$(WireEvents_<%=this.ID%>); // handle page load wiring
function WireEvents_<%=this.ID%>() {
<asp:Literal runat="server" ID="latEventToolTipJqueryScripts"></asp:Literal>
}
// ]]>
</script>
I insert the literal text dynamically from the code behind.
However, when placing the control in an updatepanel, the postbacks fail to update the script.
EDIT: The Sys.Application.add_load
successfully rewrites the necessary functions with the updatepanel. The issue lies in the script not updating within the updatepanel.
I attempted using ClientScript.RegisterStartupScript, but it yielded the same outcome as the literal control approach. Any suggestions?
---------------------------SOLVED (thanks to Pranay Rana)----------------------------------
I removed the literal from the ascx side, along with the Sys.Application.add_load
Now, all scripting is contained within the code behind. The JQuery aspect was causing confusion.
this.strBuilderJS.Append( "<script language='javascript' type='text/javascript'>" +
"$(WireEvents_" + this.ID + ");" +
"function WireEvents_" + this.ID + "(){"+
" alert('stuff');");
this.strBuilderJS.Append( "}</script>");
and then
ScriptManager.RegisterStartupScript(this, this.GetType(), "strBuilderJS", strBuilderJS.ToString(), false);