1

In my code, previously I save data when a button click, here hdnDisplayOrderSaveData is ASP hidden filed

    protected void btnSaveDisplayOrder_Click(object sender, EventArgs e)
        string data = hdnDisplayOrderSaveData.Value;
        Service service = new Service();
        if (IsSaveStringValid(data))
        {
            bool result = service.SaveServicesDisplayOrder(data);
            if (result)
            {
                ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(1);", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(2);", true);
            }
        }
        LoadDisplayOrder();
    }

Then I need to move above functionality to web method, So my code as follows,

public partial class ProductAdminSortOrder : BasePage
    {
        private List<int> serviceIds = new List<int>();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                CheckSession();
                CheckPagePermissions(Permissions.ServicesAdministrator);
                LoadDisplayOrder();
            }
            catch (Exception ex)
            {
                Logger.LogErrorEvent(ex);
                throw;
            }
        }

        /// <summary>
        /// Loads the display order.
        /// </summary>
        private void LoadDisplayOrder()
        {

        }

        [WebMethod]
        public static void SaveOrder()
        {

            string data = hdnDisplayOrderSaveData.Value;
            Service service = new Service();
            if (IsSaveStringValid(data))
            {
                bool result = service.SaveServicesDisplayOrder(data);
                if (result)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(1);", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(2);", true);
                }
            }
            LoadDisplayOrder();
        }

        //protected void btnSaveDisplayOrder_Click(object sender, EventArgs e)

        //}

        private bool IsSaveStringValid(string data)
        { 
            data = Regex.Replace(data, @"\s+", "");

            string[] items = data.Split('#');

            if (!IsDataValid(item))
            {
                return false;
            }
        }

        private bool IsDataValid(string[] item)
        {
            if (item.Length != 2)
            {
                return false;
            }
            else
            {
                return serviceIds.Any(id => id == Convert.ToInt32(item[0], CultureInfo.InvariantCulture));
            }
        }

    }

after that, I'm getting following errors,

An object reference is required for the non-static field, method, or property 'ServicesAdminSortOrder.hdnDisplayOrderSaveData'
An object reference is required for the non-static field, method, or property 'ServicesAdminSortOrder.LoadDisplayOrder()'
An object reference is required for the non-static field, method, or property 'Page.ClientScript'
  1. I have two questions, is the web method should be static
  2. How can I avoid these errors?

Updated:

how I call web method,

function DeleteKartItems() {     
     $.ajax({
     type: "POST",
     url: 'ProductAdminSortOrder.aspx/SaveOrder',
     data: "",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (msg) {
         console.log('success');
     },
     error: function (e) {
         console.log('failed');
     }
     });
}
thomsan
  • 433
  • 5
  • 19
  • The method `SaveOrder()` should not be static – Slugart Jun 16 '20 at 11:00
  • @Slugart But once I removed static part, Web method not calling – thomsan Jun 16 '20 at 11:07
  • 1
    You won't be able to access the non-static hidden field. You can send it as a part of the request to the `WebMethod` using ajax or an ASP Update Panel. – Slugart Jun 16 '20 at 11:23
  • @Slugart this web method is in aspx.cs not in a web service. so the problem is I cant call nonstatic method using Ajax. – thomsan Jun 16 '20 at 11:35
  • The other methods are not accessible because they are not static. They are part of the main page, the variables etc do not exist in the context of the webmethod, which is a totally separate request to the server. So even if you could access them they would not be populated as you expect. If you need a specific value during your web method execution you need to either need to pass it in the request parameters or fetch it from somewhere on the server such as the database or session – ADyson Jun 16 '20 at 12:45

1 Answers1

0

You will need to send the data that was previously stored in the hidden field as a part of the request to the WebMethod using ajax or an ASP Update Panel. An example: https://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx

Slugart
  • 4,535
  • 24
  • 32
  • this web method is in aspx.cs not in a web service. so the problem is I can't call the nonstatic method using Ajax. – thomsan Jun 16 '20 at 11:36
  • 1
    You are not (and never will be) able to call the non-static method from a static method. – Slugart Jun 16 '20 at 12:36