简体   繁体   English

C#错误:非静态字段需要对象引用

[英]C# error : An object reference is required for the non-static field

I've found many solutions on this but none of them make sense to me. 我已经找到了许多解决方案,但对我来说都没有意义。 Basically I'm submitting a form using jQuery AJAX and trying to update a row already in my table but get the error: An object reference is required for the non-static field. 基本上,我正在使用jQuery AJAX提交表单,并尝试更新表中已有的行,但出现错误:非静态字段需要对象引用。 If I change the "txtContactLastEdit.Text;" 如果我更改了“ txtContactLastEdit.Text;” to "New Name;" 改为“新名称”; everything is fine. 一切顺利。 It's only when I refer to the txt field on the form. 仅当我引用表单上的txt字段时。 Any ideas? 有任何想法吗? Thanks! 谢谢!

[WebMethod]
public static string updateProject(int id)
{
    using (dbPSREntities5 myEntities = new dbPSREntities5())
    {
        // Query for a specific customer. 
        var proj =
            (from tbProject in myEntities.tbProjects
             where tbProject.ProjectID == id
             select tbProject).First();

        // Change the name of the contact.
        proj.ProjectContactLastName = txtContactLastEdit.Text;

        // Ask the DataContext to save all the changes.
        myEntities.SaveChanges();

        var myResult = "success";

        return myResult;

    }
}

You can not access the page control inside the Static Method. 您不能访问静态方法内的页面控件。

You probably calling this method from client side, So one alternative is that send the txtContactLastEdit.Text from the client side and change your function to accept that paramter. 您可能从客户端调用此方法,所以一种替代方法是从客户端发送txtContactLastEdit.Text并更改您的函数以接受该参数。

[WebMethod]
public static string updateProject(int id, string textBoxValue)
{
  // your code.
}

Try to pass the value of the textbox to the static method 尝试将文本框的值传递给static方法

WebMethod]
public static string updateProject(int id, string contactName)
{
    ....

        // Change the name of the contact.
        proj.ProjectContactLastName = contatcName;


}

Inside a static method you cannot use instance variables of the class in which the static method id defined, and the txtContactLastEdit is an instance variable of the Page of type TextBox. 在静态方法内部,不能使用在其中定义了静态方法ID的类的实例变量,而txtContactLastEdit是TextBox类型的Page的实例变量。 So, supposing that the static method belongs to a class named Project, then you could call it from somewhere in your page. 因此,假设静态方法属于一个名为Project的类,则可以从页面的某个位置调用它。

int projectID = 1;
Project.updateProject(projectID,txtContactLastEdit.Text);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要对象引用才能访问C#中的非静态字段 - An object reference is required to access non-static field in C# C#中出错:“非静态字段,方法或属性需要对象引用” - Error in C#: “An object reference is required for the non-static field, method, or property” C#修复错误:“非静态字段,方法或属性需要对象引用” - C# Fixing error: “An object reference is required for the non-static field, method, or property” C#错误(使用接口方法):非静态字段,方法或属性需要对象引用 - C# error(using interface methods): An object reference is required for the non-static field, method, or property C#错误:非静态字段,方法或属性需要对象引用 - C# error: An object reference is required for the non-static field, method, or property C#错误:非静态字段,方法或属性需要对象引用 - C# Error: Object reference is required for the non-static field, method, or property 为什么我在C#中得到“非静态字段,方法或属性需要对象引用”错误? - Why am I getting “object reference is required for the non-static field, method, or property” error in C#? C# 错误:“非静态字段、方法或属性需要对象引用” - C# error: "An object reference is required for the non-static field, method, or property" C# 错误 - 非静态字段需要对象引用 - C# Error - Object reference is reqiured for the non-static field C#“非静态字段需要对象引用”,静态成员函数的类问题 - C# “An object reference is required for the non-static field,” Class issue with Static Member Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM