Show ajax query in gridview or VB.NET table

1

I'm making an AJAX application in VB.NET that shows the records of a query in a gridview, so far I can only show the data in a JSON string but I do not know how to show them in a gridview or a table.

Currently he only shows me this:

My JavaScript code is:

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnmostrar').click(function (e) {
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "Default.aspx/Mostrar",
                data: null,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (resultado) {

                 $('#msg').html(JSON.stringify(resultado));

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest + "  " + textStatus + "  " + errorThrown);
                }
            });
        });
    });        
</script>
    
asked by Drago25 10.01.2016 в 06:09
source

2 answers

-1

If you want to use gridview, modify the JS line

$('#msg').html(JSON.stringify(resultado));

To do something like this:

var row = $("[id*=msg] tr:last-child").clone(true); 
var resultados=JSON.stringify(resultado);
$("[id*=msg] tr").not($("[id*=msg] tr:first-child")).remove();
    $.each(resultados, function () {
        var res= $(this);
        $("td", row).eq(0).html(res.Id);
        $("td", row).eq(1).html(res.Nombre);
        $("td", row).eq(2).html(res.Edad));
        $("[id*=msg]").append(row); //AQUI ESTA AGREGANDO LOS RENGLONES
        row = $("[id*=msg] tr:last-child").clone(true);
    });

Here is a complete example, but basically the idea is to fill the gridview as an html table, it does not do an automatic binding, it uses it in the codebehind.

Just be careful because in the example I mentioned, use XML, not JSON, you could not use the code exactly as it looks, you have to adapt it a bit

    
answered by 11.01.2016 / 16:49
source
1

Combining javascript and json code with asp.net controls is not a good idea, mostly because asp.net does not get along with client-side code.

My recommendation is that you discard the use of the gridview if you plan to use ajax

The best way would be to use libraries like jqGrid, I do not know if you dare, but there are other alternatives such as generating some template that defines a table based on joining json with html

jQuery (jTemplates) Grid

As you can see, you can combine ajax and a template to generate the table

Remarco, if your intention is to go the way of using client code with javascript, jquery and json see thinking about setting aside asp.net and its server-side controls, I say this from experience. As recommended, you should turn to asp.net mvc, this has better support to program with client code.

    
answered by 11.01.2016 в 04:16