Jean-Emmanuel Chapartegui Jean-Emmanuel Chapartegui - 1 month ago 17
ASP.NET (C#) Question

ASP.Net javascript rebind my script after postbock

After reading this post, I understand that when you postback with updatepanel your javascript is not bound anymore.

The problem is that my javascript is on my file jscolor.js. The link between my asp page and the script is OK :

<script src="../../assets/js/jscolor.js"></script>


The classname of my textbox is "jscolor" regardin the demo on the website

<asp:TextBox Class="jscolor" ID="Couleur_1" runat="server"></asp:TextBox>


After a postback i need to re-bind my script, but I see a lot of demonstration with click function, but not with this situation.

For information, the jscolor start like this :

if (!window.jscolor) { window.jscolor = (function () { ...


Thanks in advance,
J-E

Answer

You can call the function to rebind the color to the TextBoxes like this

        if (Page.IsPostBack)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "rebuildColor", "if (!window.jscolor) { window.jscolor = (function () {", true);
        }

Or create a function that contains the re-binding to make it easier to maintain. That function will be called by the ScriptManager.

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "rebuildColor", "rebuildColor()", true);

And then in .aspx page

<script type="text/javascript">
    function rebuildColor() {
        if (!window.jscolor) { window.jscolor = (function () {...

        }
</script>