非静态场方法需要对象引用

问题描述 投票:0回答:1

我的.aspx.cs文件中有addEvent方法。我想在其中使用我的复选框和cheboxList,但是当我使用它时,会向我显示错误,如下图所示。以及我已经指定了我页面的代码,希望您能了解我所面临的问题。

我有页面上的链接,如图所示,并且在添加事件处显示错误。当我删除STATIC关键字时,我的日历添加按钮无法使用。

enter image description here

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;

}

c# asp.net checkbox checkboxlist
1个回答
0
投票

似乎WebMethod必须被标为静态。另一种方法是将静态方法中使用的所有内容都设为静态。

即,chkRecSchweekChkLabel1应该是静态的。

© www.soinside.com 2019 - 2024. All rights reserved.