![](/img/trans.png)
[英]How can I pass a function to a C# class dll, that I am calling from another C# app? The function is defined in the app where the dll is called
[英]how can function called from another class in C#
我想為我所有的按鈕單擊方法都創建一個通用類。 我的按鈕單擊方法可以在相同的aspx.cs文件中正常工作,但是當我想從通用類調用此方法時。 但是參數沒有通過。 有人可以幫幫我嗎。 這是我的代碼。
這是基類
namespace WebApplication1
{
public partial class Singnup : System.Web.UI.Page
{
protected void SUpButton_Click(object sender, EventArgs e)
{
Webapplication2.program.Insert_RData(sender, e);
}
}
}
這是第二類,我想從那里調用按鈕方法
namespace Webapplication2
{
public class program : WebApplication1.Singnup
{
public static void Insert_RData(object sender, EventArgs e)
{
SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
con_Signup.Open();
SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
cmd_check.CommandType = CommandType.StoredProcedure;
cmd_check.Parameters.AddWithValue("@mail",EmailId);
object i = cmd_check.ExecuteScalar();
if (i != null)
{
lbforerror.Text = "This Email is already Registered";
lbforerror.Visible = true;
}
嘗試下面的代碼
namespace WebApplication1
{
public partial class Singnup : System.Web.UI.Page
{
protected void SUpButton_Click(object sender, EventArgs e)
{
string EmailId = "yourmailid@domain.com";
Webapplication2.program.InsertRData(EmailId);
}
}
}
namespace Webapplication2
{
public class program : WebApplication1.Singnup
{
public static void Insert_RData(object sender, EventArgs e)
{
string EmailId = "yourmailid@domain.com";
InsertRData(EmailId);
}
public static void InsertRData(string EmailId)
{
SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
con_Signup.Open();
SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
cmd_check.CommandType = CommandType.StoredProcedure;
cmd_check.Parameters.AddWithValue("@mail", EmailId);
object i = cmd_check.ExecuteScalar();
if (i != null)
{
lbforerror.Text = "This Email is already Registered";
lbforerror.Visible = true;
}
}
}
}
您正在將信息從實例化的Singup
類Singup
到靜態類program
。 標簽等將無法從靜態類獲得,因為它與實際標簽沒有任何關系。
如果您想分開按鈕單擊動作,並且希望它們訪問表單元素(例如lbforerror
那么我建議您使用部分類或僅在同一類內使用Region來清理邏輯)
#region Button Logic
//Your logic here
#endregion //button logic
最好的方法是將可重用邏輯分為一個完全不同的類(在這種情況下,處理插入),該類返回結果,並由您的原始類決定:
public class ConnectionManager
{
public object InsertRData(string EmailId)
{
SqlConnection con_Signup = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
con_Signup.Open();
SqlCommand cmd_check = new SqlCommand("Check_Existing_Email", con_Signup);
cmd_check.CommandType = CommandType.StoredProcedure;
cmd_check.Parameters.AddWithValue("@mail", EmailId);
object i = cmd_check.ExecuteScalar();
return i;
}
}
那你原來的課可以簡單地說
public partial class Singnup : System.Web.UI.Page
{
protected void SUpButton_Click(object sender, EventArgs e)
{
ConnectionManager mgr = new ConnectionManager();
object i = mgr.Insert_RData("email logic here");
if (i != null)
{
lbforerror.Text = "This Email is already Registered";
lbforerror.Visible = true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.