Referencing non-static object in a static method

Asked

Viewed 272 times

0

Good evening, I need to reference a component PlaceHolder Asp.Net in a static method in the code-behind C#, this method is called via AJAX for front-end see the code:

Method call by the Form aspx front-end:

<script type="text/javascript">
    function CallCsharpFunction(andamento_id) {
         try {
            $.ajax({
                type: "POST",
                url: 'Processo.aspx/AtuarNoProcesso',
                data: "{'AndamentoID':'" + andamento_id + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: location.reload(true), 
                error: function (msg) {
                    alert(msg.d);
                }
            });
        } catch (err) { }
    }
</script>

The Method in Code-Behind C#:

[System.Web.Services.WebMethod]
public static string AtuarNoProcesso(int AndamentoID)
{   
    PlaceHolder1.Controls.Add(new Literal
    {
          Text =  Andamento.Make_Table_Html(Convert.ToInt32(AndamentoID)) });
    }
}

But get the message "An object reference is required for the non-static field, method, or property" in the component PlaceHolder1. How do I make the method see the Placeholder1 component?

  • No way !!!! ..... will have to use another way! what you want to do?

  • @Diegorafaelsouza Andamento is a class with a function that returns a "Make_table_html" string that mounts html to a table with people names

  • 1

    If the method needs to access an instance member it should not be static.

  • 1

    @Evander returns a JSON and work the information on your front, since you are using Ajax! directly this is not possible, or even returns the html changing the return dataType:'html' in his ajax! have two options there.

  • @Virgilionovic I will try to model returning a JSON, you would have an example in code to guide me.

  • 1

    @Evandro an example: https://cbsa.com.br/post/web-service--retornar-json-com-aspnetc.aspx

  • 1

    @Virgilionovic so it will work in return $('.resultado').text(data.d); I assemble html, I will test and put here

Show 2 more comments

1 answer

0


According to the staff tip I set the table html with the information in return JSON of the call Ajax, see how it turned out:

<script type="text/javascript">
    function GetData(andamento_id) {
        var $tbl = $('#tbl');
        $.ajax({
            url: 'Andamento.aspx/GetComentarioAndamento',
            data: "{'AndamentoID':'" + andamento_id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            type: "POST",
            success: function (data) {
                debugger;
                if (data.d.length > 0) {
                    for (var i = 0; i < data.d.length; i++) {
                        $tbl.append('<tr><td>' + data.d[i].ComentarioTexto
                            + '</td><td>' + data.d[i].DataHoraComentario
                            + '</td><td>'
                            + "<div class='row centered'><div class='user-block'><img src='"
                            + data.d[i].FotoProfile + "' data-toggle='tooltip' title='"
                            + data.d[i].NomeAdvogado + "' class='img-circle img-bordered-sm'></div></div>"
                            + '</td></tr>');
                    }
                }
            }
        });
    }
</script>

//tabela
<table id="tbl" class="table table-hover">
   <tr>
   </tr>
</table>

Browser other questions tagged

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