简体   繁体   English

如何从C#中的另一个类调用函数

[英]how can function called from another class in C#

I want to make one generic class for all my button click methods. 我想为我所有的按钮单击方法都创建一个通用类。 My button click method works properly in same aspx.cs file but when I want to call this method from generic class. 我的按钮单击方法可以在相同的aspx.cs文件中正常工作,但是当我想从通用类调用此方法时。 But parameter is not passing. 但是参数没有通过。 Can someone please help me. 有人可以帮帮我吗。 Here is my code. 这是我的代码。

This is base class 这是基类

namespace WebApplication1
{
    public partial class Singnup : System.Web.UI.Page
    {
        protected void SUpButton_Click(object sender, EventArgs e)
        {
            Webapplication2.program.Insert_RData(sender, e);
        }
    }
}

This is 2nd class where from i want to call button method 这是第二类,我想从那里调用按钮方法

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;
            }

try below code 尝试下面的代码

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;
            }
        }
    }
}

You are passing information from an instantiated class Singup to a static class program . 您正在将信息从实例化的SingupSingup到静态类program The labels, etc will not be available from a static class, as it has no connection to the actual label. 标签等将无法从静态类获得,因为它与实际标签没有任何关系。

If you want to separate the button click actions, AND you want them to access form elements (such as lbforerror then i suggest you use a partial class or simply use a Region within the same class to clean up the logic 如果您想分开按钮单击动作,并且希望它们访问表单元素(例如lbforerror那么我建议您使用部分类仅在同一类内使用Region来清理逻辑)

#region Button Logic
//Your logic here
#endregion //button logic

The best way, is to separate the re-useable logic into a completely different class (in this case, handling the insertion) which returns the result, and is decisioned in your original class: 最好的方法是将可重用逻辑分为一个完全不同的类(在这种情况下,处理插入),该类返回结果,并由您的原始类决定:

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;
    }
}

Then your original class can simply say 那你原来的课可以简单地说

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM