locked
an object reference is required for the non-static field RRS feed

  • Question

  • User-1241097802 posted

    I have checkbox and checkbox list in my .aspx page i want to use it inside the ADD EVENT Method but show me error. here i my code. Inside the "public static int addEvent" i want to use the checkbox and checkboxlist<br/>

    protected void chkRecSch_CheckedChanged(object sender, EventArgs e)
    {
    if (chkRecSch.Checked == true) {
    //BindCheckBoxList();
    BindCheckBoxListWithNextSevenDays();
    weekChk.Visible = true;
    }
    else { weekChk.Visible = false; }
    
    }
    
    public void BindCheckBoxListWithNextSevenDays()
    {
    DateTime today = DateTime.Now;
    
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("date", typeof(string));
    
    for (int i = 1; i <= 7; i++)
    {
    dt.Rows.Add(i, today.AddDays(i).ToString("dddd yyyy-MM-dd")); // (dddd yyyy-MM-dd) will return the date in following format (Wednesday 2020/02-05)
    }
    chkweek.DataSource = dt;
    chkweek.DataBind();
    }
    
    [System.Web.Services.WebMethod]
    public static int addEvent(ImproperCalendarEvent improperEvent)
    {
    if (chkRecSch.Checked == true)
    {
     foreach (ListItem li in weekChk.Items)
     {
     if (li.Selected == true)
     {
     Label1.Text += li + " ";
     }
     }
    }
    
    CalendarEvent cevent = new CalendarEvent()
    {
    
    title = "Schedular Master",
    description = "Schedular Master",
    
    StartDate = improperEvent.start,
    StartTime = improperEvent.startTime,
    
    EndDate = improperEvent.end,
    
    EndTime = improperEvent.endTime,
    
    
    UserID = System.Web.HttpContext.Current.Session["AdminID"].ToString(),
    ProviderId = improperEvent.ProviderId
    
    };
    
    if (CheckAlphaNumeric(cevent.title) && CheckAlphaNumeric(cevent.description))
    {
    int key = EventDAO.AddScheduleMaster(cevent);
    
    return key;
    
    }
    
    return -1;
    
    }

    Wednesday, February 5, 2020 9:42 AM

Answers

  • User753101303 posted

    For example you have a sample here: https://www.itprotoday.com/web-application-management/calling-server-side-page-method-client-side-javascript (it could be called also without using the script manager). A web method MUST be static and can't access anything which is instance level.

    As you can see from this sample it doesn't interact at all with the page server side as anyway the page class is not created at all. Instead it just pass a parameter and get back data which are used to update the UI from the client side. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static for more about static.

    Depending on what you want, a regular post back could be enough for now. If you really want to update a part of a page you could perhaps try https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.updatepanel?view=netframework-4.8#introduction (though sometimes I find easier to do that yourself if you are comfortable with being on your own).

    More likely for now it could be better to start with a test page so that you can experiment with whatever you try to do...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2020 11:48 AM
  • User288213138 posted

    Hi programming,

    Inside the "public static int addEvent" i want to use the checkbox and checkboxlist

    [System.Web.Services.WebMethod]
    public static int addEvent(ImproperCalendarEvent improperEvent)
    {
    if (chkRecSch.Checked == true)
    {
     foreach (ListItem li in weekChk.Items)
     {
     if (li.Selected == true)
     {

    It's impossible to get the asp control in webmethod. remember at this time the code that called the WebService is located in the user’s machine somewhere in the world, and the WebService is located in your server, to show the result in the client you need to use JavaScript and the DOM in the success callback function to update.

    When a class method is "static", that means it doesn't automatically have access to a particular instance of the class.  When your static method is called, the controls on your page literally do not exist anywhere on the server-side, making it impossible for the static method to access them on the server-side.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 6, 2020 8:23 AM

All replies

  • User753101303 posted

    Hi,

    Your static web method is using page instance properties which is not possible (as the method is static you have no current page instance with which you could interact).

    Instead you should pass explicitly to this web method whatever is needed (such as for example values for some input fields) and return data as well that could be then be used to update the UI on the client side.

    The page itself doesn't "run" at all during a web page method call. In short this is just a shortcut for providing a web service to the rendered client side HTML/CSS/JavaScript page.

    Wednesday, February 5, 2020 9:58 AM
  • User-1241097802 posted

    Sir do you have any reference code because i am fresher and this is running project when i remove the static my ADD Button not able to insert the data inside DB Table.

    Wednesday, February 5, 2020 10:18 AM
  • User753101303 posted

    For example you have a sample here: https://www.itprotoday.com/web-application-management/calling-server-side-page-method-client-side-javascript (it could be called also without using the script manager). A web method MUST be static and can't access anything which is instance level.

    As you can see from this sample it doesn't interact at all with the page server side as anyway the page class is not created at all. Instead it just pass a parameter and get back data which are used to update the UI from the client side. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static for more about static.

    Depending on what you want, a regular post back could be enough for now. If you really want to update a part of a page you could perhaps try https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.updatepanel?view=netframework-4.8#introduction (though sometimes I find easier to do that yourself if you are comfortable with being on your own).

    More likely for now it could be better to start with a test page so that you can experiment with whatever you try to do...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2020 11:48 AM
  • User288213138 posted

    Hi programming,

    Inside the "public static int addEvent" i want to use the checkbox and checkboxlist

    [System.Web.Services.WebMethod]
    public static int addEvent(ImproperCalendarEvent improperEvent)
    {
    if (chkRecSch.Checked == true)
    {
     foreach (ListItem li in weekChk.Items)
     {
     if (li.Selected == true)
     {

    It's impossible to get the asp control in webmethod. remember at this time the code that called the WebService is located in the user’s machine somewhere in the world, and the WebService is located in your server, to show the result in the client you need to use JavaScript and the DOM in the success callback function to update.

    When a class method is "static", that means it doesn't automatically have access to a particular instance of the class.  When your static method is called, the controls on your page literally do not exist anywhere on the server-side, making it impossible for the static method to access them on the server-side.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 6, 2020 8:23 AM