0

可能重复:
从 WebMethod 访问函数背后的代码

我有以下 WebMethod,我通过 Json 调用它。它工作正常。

   [System.Web.Services.WebMethod]
public static string CheckUserName(string userName)
{
    string returnValue = string.Empty;
    try
    {
        string consString = ConfigurationManager.AppSettings["cn"].ToString();
        SqlConnection conn = new SqlConnection(consString);
        SqlCommand cmd = new SqlCommand("UsernameCheck", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@username", userName.Trim());
        conn.Open();
        returnValue = cmd.ExecuteScalar().ToString();
        conn.Close();
    }
    catch
    {
        returnValue = "error";
    }
    return returnValue;
}

但正如我们所知,我们无法在静态函数中访问页面控件和函数背后的代码。

但是在这里我必须访问 WebMethod 中函数背后的代码。IE

public void test()
  {
   }

请帮助我如何访问 webmethod 中的“测试”功能。在测试功能中,我使用了两个中继器控件和数据表。

谢谢你。

4

1 回答 1

0

I understood you. But what you trying to do is not possible that way.

The webmethod is returned to a script inside the page, I guess, so you are not able to call another instance method from page.

What you may try is to do a postback to the same page after the result, and call the test() method again. Then you will be able to refresh your repeaters and datatable.

Another solution is to return the repeater as html by the webmethod.

于 2012-06-21T06:42:12.193 回答