0

I have a WebMethod in my aspx.cs which is being called on the client side via jquery using an Ajax POST Request when an ASP:Label is being clicked.

The WebMethod points to another method in the aspx.cs and actually retrieve a DataTable which is used to populate the Gridview and then display it.

WebMethod and other method (aspx) :

    [System.Web.Services.WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static void ViewDetailsItemOrdersWS(String ddlTimeSelectedVal)
    {
        Index i = new Index();
        i.itemOrder(ddlTimeSelectedVal);
    }


    protected void itemOrder(String ddlTimeSelectedVal) {

        //try
        //{
        int val = Convert.ToInt32(ddlTimeSelectedVal);
        //PlaceHolder placeholder1 = new PlaceHolder();
        //placeholder1.Controls.Add(gvList);
        DataTable s = new DataTable();
        s = getMenuData(val);

        Debug.WriteLine(s.Rows.Count);

        gvList.DataSource = s;

        gvList.DataBind();
        gvList.Visible = true;
        //}
        //catch (Exception ex)
        //{
        //    Debug.WriteLine(ex.ToString());
        //}

    }

Client's side codes :

            $(document).ready(function () {

            var dataDDLTime = $('.<%= ddlTime.ClientID %>').val();

            $('#<%= lblItemOrders.ClientID %>').on('click', function () {

                var dataOfDDLTime = JSON.stringify({ ddlTimeSelectedVal: dataDDLTime });

                alert(dataOfDDLTime);

                if ($('[id$=gvList]') != undefined) {
                    $.ajax({
                        type: "POST",
                        url: "Index.aspx/ViewDetailsItemOrdersWS",
                        data: dataOfDDLTime,
                        contentType: 'application/json; charset=utf-8',
                        dataType: "json",
                        success: function (data) {

                        },
                        error: function (err) {
                            console.log(err);
                        }
                    });
                } else {
                    console.log('gvList not found.');
                }
            });
        });

I got this error. I checked the DataTable returned, apparently the number of rows isn't 0. I can't tell what's the issue with the Gridview being null.

error img

Appreciate any help, thanks!

domster
  • 504
  • 1
  • 6
  • 20
  • Either `getMenuData` returns NULL or `gvList` idoes not exist. – VDWWD Jan 14 '18 at 15:13
  • i guess it's gvList.. but i don't get it. document.ready checks for all the elements to be loaded before performing any functions inside it.. and getMenuData does return a datatable with a number of rows, means that there's data inside and it isn't null. thanks! – domster Jan 14 '18 at 15:14
  • document.ready? That is a front-end thing. The error comes from the backend. The problem is that you are expecting the aspx Controls to be available in a `WebMethod`. They are not. – VDWWD Jan 14 '18 at 15:16
  • oh.. even if i do check, i won't be able to solve it.. is there a workaround to fixing what i'm doing wrongly? – domster Jan 14 '18 at 15:17
  • Either return a json and process that or get the data directly on Page Load and bind it to the GridView. You can call `ItemOrder` within an IsPostBack check without the ajax call loop. – VDWWD Jan 14 '18 at 15:19
  • Sweet, thanks man I get what you mean. I'll try it out and get back here. Cheers!! – domster Jan 14 '18 at 15:27

0 Answers0