![](/img/trans.png)
[英]How to call function from class file (.cs) file to another code behind file (.aspx.cs)?
[英]how to call a function defined in a class file on aspx.cs page
這是寫在類文件Delete.cs上的代碼,我想在[WebMethod]下的WebForm1.aspx上訪問此代碼。
namespace Bootstrap
{
public class DeleteData
{
public static string DeleteData(int rollno)
{
string msg = string.Empty;
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spDeleteData", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@RollNo", rollno);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i == 1)
{
msg = "true";
}
else
{
msg = "false";
}
}
return msg;
}
}
}
在我的webform1.aspx中,我這樣稱呼它
[WebMethod]
DeleteData delete = new DeleteData();
delete.DeleteData(int rollno);
他們兩個都使用相同的命名空間,而我的Delete.cs在App_Code文件夾中,但是它給出了編譯時錯誤。 請幫忙
哇,您設法使每件事都錯了,可能是錯的。 您是否實際編寫了DeleteData方法? 好像您忘記了已經知道的一切。
您需要一種方法,而不僅僅是代碼。 在該方法中,您需要調用您編寫的類的方法。 而且它是靜態的,因此您不需要它的實例。
[WebMethod]
public string DeleteData(int rollno)
{
return DeleteData.DeleteData(rollno);
}
不過,您應該進行命名工作,這很令人困惑。
無需實例即可調用靜態方法。
[WebMethod]
public returntype MethodName()
{
//lines of code
int rollno=YourValue;
DeleteData.DeleteData(rollno);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.