非静态字段,方法或属性'_Default.Label1'需要对象引用? [英] An object reference is required for the non-static field, method, or property '_Default.Label1'?
本文介绍了非静态字段,方法或属性'_Default.Label1'需要对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我是c#中的新手,我来自php ..
我收到此错误:非静态字段需要对象引用,方法或属性'_Default.Label1'
当我尝试运行时:
Hi All.
Im a newbee in c#, i come from php..
I get this error: An object reference is required for the non-static field, method, or property '_Default.Label1'
When i try to run this:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
[WebMethod]
public static void SendForm(int name)
{
name = name * 100;
name.ToString();
Label1.Text = name;
/*
if (string.IsNullOrEmpty(email))
{
throw new Exception("You must supply an email address.");
}
if (string.IsNullOrEmpty(message))
{
throw new Exception("Please provide a message to send.");
}
*/
// If we get this far we know that they entered enough data, so
// here is where you would send the email or whatever you wanted
// to do :)
}
}
推荐答案
这是因为SendForm()被定义为静态方法。这意味着它无法访问任何每个实例的数据。
如果删除"静态"一词。从它的声明,它将工作。
It's because SendForm() is defined as a static method. That means that it does not have access to any per-instance data.
If you remove the word "static" from its declaration, it'll work.
这篇关于非静态字段,方法或属性'_Default.Label1'需要对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文