C# ASP.NET C将方法移动到类文件并从代码隐藏中调用它

C# ASP.NET C将方法移动到类文件并从代码隐藏中调用它,c#,asp.net,web-services,C#,Asp.net,Web Services,我有以下代码在我的aspx代码背后,它工作良好。因为我将在多个页面上使用它,所以我想将它移动到类文件中。我不知道如何正确地做到这一点 #region autocomplete search [WebMethodAttribute(), ScriptMethodAttribute()] public static string[] GetCompletionList(string prefixText, int count, string contextKey) { SqlConnect

我有以下代码在我的aspx代码背后,它工作良好。因为我将在多个页面上使用它,所以我想将它移动到类文件中。我不知道如何正确地做到这一点

#region autocomplete search

[WebMethodAttribute(), ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
    SqlConnection con;
    SqlCommand cmd;
    string cmdString = "SELECT TOP(15) Title FROM posts WHERE (Title LIKE '%" + prefixText + "%') OR (Content LIKE '%" + prefixText + "%')";
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);

    cmd = new SqlCommand(cmdString, con);
    con.Open();

    SqlDataReader myReader;
    List<string> returnData = new List<string>();

    myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    while (myReader.Read())
    {
        returnData.Add(myReader["Title"].ToString());
    }

    myReader.Close();
    con.Close();

    return returnData.ToArray();
}

#endregion
我试着从代码隐藏页面这样调用它:

BlogFrontUtil.GetCompletionListprefixText,count,contextKey

……但它不起作用,我看到红色的曲线。错误消息表示它是一个方法,但与类型一样使用

谁能教我怎么做才合适。我的经验有限


谢谢

在新课程中添加方法:

public static class NewClass
{
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        SqlConnection con;
        SqlCommand cmd;
        string cmdString = "SELECT TOP(15) Title FROM posts WHERE (Title LIKE '%" + prefixText + "%') OR (Content LIKE '%" + prefixText + "%')";
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);

        cmd = new SqlCommand(cmdString, con);
        con.Open();

        SqlDataReader myReader;
        List<string> returnData = new List<string>();

        myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        while (myReader.Read())
        {
            returnData.Add(myReader["Title"].ToString());
        }

        myReader.Close();
        con.Close();

        return returnData.ToArray();
    }
}

请发布您的代码隐藏文件中的确切代码段。请发布您也会收到的错误嗨,上面的代码就是确切的代码,当我将鼠标悬停在红色的曲线上时,错误消息会显示出来。我正在ascx文件中使用asp.net ajax自动完成扩展程序。我在aspx文件代码中使用的这个小片段。因为AutoCompleteXtender不喜欢ascx文件,所以这是我能使其工作的唯一方法。Dustin,谢谢!现在我明白我犯的错误了。另外,感谢您理解我的问题,并提供了我想要的简单答案。
[WebMethodAttribute(), ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
    string[] s = NewClass.GetCompletionList(prefixText, count, contectKey);
    return s;
}