Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic Javascript in asp.net

well. The problem i'm facing is i have my own custom control and i do a query , get records and dynamically add html controls to the page based on the data.

Now there's the problem of adding some dynamic javascript

I do this with the help of literal controls.

<asp:Literal runat="server" ID="latEventToolTipJqueryScripts"></asp:Literal>

This works like a charm

<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 add the literal text dynamically from code behind.

However, when placing the control in an updatepanel, the postbacks don't update the script.

EDIT: The Sys.Application.add_load rewires the necessary functions just fine with the updatepanel. The problem is that the script that needs to be in place of the literal, doesn't update when in an updatepanel.

I've tried the ClientScript.RegisterStartupScript but it has the same effect as with the literal control trick. Any help?

---------------------------SOLVED (tnx to Pranay Rana)----------------------------------

Got rid of the literal in the ascx side. as well as the Sys.Application.add_load

now it's only in the code behind. The thing that was throwing me off was the JQuery thing.

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);
like image 915
robert Avatar asked Jun 02 '11 06:06

robert


2 Answers

Make use of ScriptManager.RegisterStartupScript() to register your script...may resolve problem ...

Check this resolve your problem : Add JavaScript programmatically using RegisterStartupScript during an Asynchronous postback

like image 174
Pranay Rana Avatar answered Nov 07 '22 01:11

Pranay Rana


I would recommend on a different approach. To create a dynamic javascript for any language. I would create a reference to a dynamic file. For this example I will use .NET

  1. Create a test.aspx page and add this script:

    <script type="text/javscript" src="js/foo.aspx"></script>

  2. Make sure your page response with the right content type. So for this instance I would add this on page load code behind for your foo.aspx:

    Response.ContentType = "application/javascript";

  3. On the foo.aspx html view add your javascript code.

    alert("<%=DateTime.Now%>");

  4. Browse your test.aspx page and you should see an alert with the current date

You should be able to move forward from here. The goal is to separate the javascript file from the static page.

Happy Coding.

like image 3
jimmyo Avatar answered Nov 07 '22 01:11

jimmyo