如何将类文件(.cs)文件中的函数调用到文件后面的另一个代码(.aspx.cs)?

时间:2014-01-09 11:55:04

标签: c# .net

  public class reference
  {
     public reference()
     {
       //
       // TODO: Add constructor logic here
       //
      }  

 public void login(object sender, EventArgs e)
 {
    if (rblustp.SelectedIndex == 1)
    {
        Session["UserName"] = txt_un.Text;
        string select = "select count(*) from userlogin where username='" + txt_un.Text + "'and password = '" + txt_pass.Text + "'";
        con = new SqlConnection("Data Source=VINTECH-PC;Initial Catalog=example;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = new CommandType();
        cmd.CommandText = select;
        SqlParameter username = new SqlParameter("@username", SqlDbType.VarChar, 50);
        username.Value = txt_un.Text.Trim().ToString();
        cmd.Parameters.Add(username);
        SqlParameter password = new SqlParameter("@password", SqlDbType.VarChar, 50);
        password.Value = txt_pass.Text.Trim().ToString();
        cmd.Parameters.Add(password);
        con.Open();
        int result = (Int32)cmd.ExecuteScalar();
        con.Close();
        if (result >= 1)
        {
            Response.Redirect("Edituserprofile.aspx");
        }
       else
        {
            MessageBox.Show("You are not a registered user");
        }
    }
    else
    {
        if (txt_un.Text == "viewpine" && txt_pass.Text == "administrator" && rblustp.SelectedIndex == 0)
        {

            SqlDataAdapter da = null;
            da = new SqlDataAdapter("select count(*) from adminreg where username = '" + txt_un.Text + "' and password = '" + txt_pass.Text + "'", con);

            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Redirect("userprofile.aspx");
            }
        }
        else
        {
            MessageBox.Show("You are not an administrator");
        }
    }
}

public void Main(object sender,EventArgs e)
{
    reference r = new reference();
    r.login();
}

这是一个类文件。现在我如何在其他aspx.cs页面中调用login()?此外,它为txt_passtxt_un提供了错误,即上下文中不存在此错误。

如何删除这些错误?

请详细说明,因为我是新手。还有如何以及在何处创建类的实例?

2 个答案:

答案 0 :(得分:2)

简单的代码朋友......

string user_name="Some_user",password="correct_password";

login(user_name,password)
{
 class_name object=new class_name();
 if(true==object.methode_name(user_name,password))
           //        do_something
 else
           //        do_something
}

在您的班级文件中

 class class_name
 {
    public bool methode_name(string user_name,string password)
    {
        //your code here 
        if(/*yout code here to validate user*/)
            return true;
        else
            return false;

    }
}

答案 1 :(得分:1)

将这些方法放在一个类中。确保内部的所有方法都是public static,否则您将无法从外部访问它们。

using System;
using ...
using ...

public static class MyBigClass
{
   ...
   methods
   ...
}

将所有这些代码保存为.cs文件,例如您应用的MyBigClass.cs文件夹中的App_Code

现在您可以从任何其他文件访问这些方法:

MyBigClass.MyMethod(...);
相关问题