[英]How to call function from class file (.cs) file to another code behind file (.aspx.cs)?
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_pass
和txt_un
錯誤,該錯誤不在上下文中。
如何清除這些錯誤?
請詳細告知,因為我是開發的新手。 還有如何以及在哪里創建類的實例?
簡單的代碼朋友...
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;
}
}
將這些方法放在一個類中。 確保內部的所有方法都是public static
,否則您將無法從外部訪問它們。
using System;
using ...
using ...
public static class MyBigClass
{
...
methods
...
}
將所有這些代碼另存為.cs
文件,例如,應用程序的App_Code
文件夾中的MyBigClass.cs
。
現在,您可以從任何其他文件訪問這些方法:
MyBigClass.MyMethod(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.