Execute a Javascript from code-behind .net 4.5

0

I am developing a management process to a database from a webservice and since the process takes more than 20 minutes I need to show the user messages in a <textarea> in a part of the page, without updating the page for this place a javascript on the page that when executed adds a line of text (which happened by parameter) to said <textarea> from a EventListener that listens to the events that I generate from the code who is responsible for doing the management every time I want to show a message on screen.

The problem is that Javascript is never executed, and I'm calling it like this: ClientScript.RegisterStartupScript(this.GetType(),"mandarmensaje", "Feed(\'" + sender.ToString() + "\');", true);

I already test the javascript by running from a button and if it works well and when I debug the command it "executes" without error but does nothing, and I put alert(); in the script to see if it at least comes in and neither.

The Javascript

<script type="text/javascript" language="javascript">
    function Feed(msg) {
       msg.replace(/@/g, "\r");         
       document.getElementById("feedbackText").value = document.getElementById("feedbackText").value + "\r" + msg;
    }  
</script>
  

The replace is because I pass line breaks like "@" and then I convert them again, so as not to have problems with the web interpreter.

I've been in this for 2 days, thanks to who can help me.

    
asked by Jesus Hergueta 23.08.2017 в 20:56
source

1 answer

0

As far as I can understand, you perform a process on the server that takes some time and you need to run a JavaScript on the client, avoiding postback .

I do it like this:

I put a script on the ASPX page for example, which runs on the server and I put the runat=server tag on it and on that same page I place a script that is run on the client side.

In this way the function on the server gives the command to run on the browser side.

This code works like this, (to give more clarity) when loading the page the Page_load event organizes that when the user clicks on the Procesar button, two processes are executed simultaneously, one on the server that performs queries long to a database and as it takes a long time, the other event (the one that records Page_load ) executes the script on the client side that shows an animation that tells the user that it should wait.

This is the code:

<%--Codigo JavaScript--%>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAceptar.Attributes.Add("OnClick", "javascript:return fnAceptar();");
    }
</script>


<script language="javascript" type="text/javascript">
    function fnAceptar() {
        alert('El Contenido del TextBox es: ' + document.getElementById("txtNombre").value);
        document.getElementById("txtNombre").value = '';
    }
</script>

This link may be useful.

    
answered by 23.08.2017 / 21:20
source