[英]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();
}
This is a class file. 这是一个类文件。 Now how I call login()
in other aspx.cs page? 现在我如何在其他aspx.cs页面中调用login()
? Also it is giving error for txt_pass
and txt_un
that this is not present in the context. 另外,它给出了txt_pass
和txt_un
错误,该错误不在上下文中。
How to remove these errors? 如何清除这些错误?
Please tell in detail because I am new to development. 请详细告知,因为我是开发的新手。 Also how and where to create instance of a class? 还有如何以及在哪里创建类的实例?
Simple code friend... 简单的代码朋友...
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
}
in your class file 在您的课程文件中
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;
}
}
Plase these methods in a class. 将这些方法放在一个类中。 Make sure all methods inside are public static
, otherwise you won't be able to access them from outside. 确保内部的所有方法都是public static
,否则您将无法从外部访问它们。
using System;
using ...
using ...
public static class MyBigClass
{
...
methods
...
}
Save all this code as a .cs
file, eg MyBigClass.cs
in App_Code
folder of your app. 将所有这些代码另存为.cs
文件,例如,应用程序的App_Code
文件夹中的MyBigClass.cs
。
Now you can access these methods from any other file: 现在,您可以从任何其他文件访问这些方法:
MyBigClass.MyMethod(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.