Javascript call from within code Behind does not work

Asked

Viewed 827 times

0

This is my job:

<script type="text/javascript">
        function montaDataSubstituicaoPrestador(dt_exclusao) {

            var arrData = dt_exclusao.split('/');
            var exclusaoFormatada = arrData[1] + '-' + arrData[0] + '-' + arrData[2];
            var dias = parseInt(prazoSubPrestador);
            var novaData = new Date(arrData[2], arrData[1] - 1, arrData[0]);

            novaData.setDate(novaData.getDate() + dias);

            hoje = new Date(novaData)
            dia = hoje.getDate()
            mes = hoje.getMonth()
            ano = hoje.getFullYear()
            if (dia < 10)
                dia = "0" + dia

            if ((mes + 1) < 10)
                mes = "0" + (mes + 1);

            if (ano < 2000)
                ano = "19" + ano

            var dt = dia + "/" + (mes) + "/" + ano;

            document.getElementById('lblPrazoSubsAns').innerHTML = "Prazo de substituição: " + dt;
        }
    </script>

And this is my call:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(), "MontaDataExclusaoPrazoANS", "montaDataSubstituicaoPrestador(calDataExclusao.Date)", true);

What happens is that nothing goes up, the label is not changed. The function works, but not this way. I tried to call directly in aspx, but it did not work. Below my aspx and do not know where to call. Just combo code to call the function.

<table width="100%">
                    <tr>
                        <td style="width:30%; text-align:right">Motivo:</td>
                        <td style="width:70%"><asp:DropDownList ID="ddlMotivoExclusao" runat="server" Width="400px" OnSelectedIndexChanged="ddlMotivoExclusao_OnSelectedIndexChanged" AutoPostBack="true"></asp:DropDownList></td>
                    </tr>
                    <tr id="trDataNotificacaoExclusaoVoluntaria" runat="server" visible="false">
                        <td style="text-align:right">Data Notificação da Exclusão Voluntária:<span style="color:Red">*</span></td>
                        <td><cc1:Calendario id="calDataNotificacaoExclusaoVoluntaria" runat="server" AutoPostBack="false" /></td>
                    </tr>
                    <tr>
                        <td style="text-align:right">Data Exclusão:</td>
                        <td>
                            <cc1:Calendario id="calDataExclusao" runat="server" AutoPostBack="true" OnTextChanged="calDataExclusao_TextChanged" />
                            <asp:Label runat="server" ID="lblPrazoSubsAns">Teste:</asp:Label>
                            <asp:HiddenField ID="hidQtdeDiasExclusao" runat="server" />
                            <asp:HiddenField ID="hidIndLEI13003" runat="server" />
                        </td>
......
  • Is the call coming to JS? Another point, I saw that you are passing the parameter in JS in a strange way, so try Cs.Registerclientscriptblock(this.Gettype(), "Montadataexclusaoprazoans", "montaDataSubstituicaoPrestador("+Datetime.Now+")", true);

  • Yes.. But the way you’re going, it’s going as STRING and not as DATA.

  • Calm down... what do you want to do? Say it in a nutshell. What you did comes to call the Javascript method?

  • I need to load a date coming oracle. This date is a composition with data_delete + a parameterized value. In the database this is ok. In Asp is also working and in MVC. But not in aspx. See that the system is a patchwork. I can’t call the function, but the js function I passed has error and it has already corrected and still hasn’t worked. This is my need.

  • Right, When you do this -> Cs.Registerclientscriptblock(this.Gettype(), "Montadataexclusaoprazoans", "montaDataSubstituicaoPrestador(calDataExclusao.Date)", true); The function arrives in Javascript?

  • @Paulohdsousa, gives this error that I can not go where the error is: Unexpected token ILLEGAL. The script is not being called. I put a alert(); in the first line and nothing.

  • Try calling Cs.Registerclientscriptblock(this.Gettype(), "Montadataexclusaoprazoans", "montaDataSubstituicaoPrestador(null)", true);

  • Nothing, didn’t fire the alert();.

  • Try calling Cs.Registerclientscriptblock(this.Gettype(), "Montadataexclusaoprazoans", "<script>montaDataSubstituicaoPrestador(null)</script>", true);

  • @Paulohdsousa, man is bone the business here. Also not fired the alert();. Alert is the first line of the function.

  • Just for the record... you get to run the line below ? Cs.Registerclientscriptblock(this.Gettype(), "Montadataexclusaoprazoans", "montaDataSubstituicaoPrestador(calDataExclusao.Date)", true);

  • Yes, I go with F10 and it goes through the line yes. However, I got it a lot earlier with javascript, due to the Chrome cache. And I was only able to solve it by restarting the machine. Ultimately, I will do this.

  • My last card, see if it works like this Scriptmanager.Registerclientscriptblock(this, this.Gettype(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador(null);", true);

  • @Paulohdsousa, in this way funfou. The Alert(); went up. Now I will debug and know why did not change the label, but this is something else. The important thing is that the function was called. For the title of the question, your suggestion worked. Put as an answer to the post.

Show 9 more comments

1 answer

1


Try it like this and see how it works

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador(calDataExclusao.Date);", true); 
  • it just doesn’t accept when I concaten a variable like this: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador(" + nova_data + ");", true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.