在asp.net中的另一个.cs文件中包含一个代码隐藏文件

时间:2022-11-30 02:02:22

I am writing a code, and i have a web form called Default.aspx and have a code behind file Default.aspx.cs and added some textboxes, buttons etc. to it. Now i created another .cs item called University.cs and want to include Default.aspx in University.cs to use the values in the textboxes. Here is the code for Default.aspx.cs:

我正在编写代码,我有一个名为Default.aspx的Web表单,并在文件Default.aspx.cs后面有一个代码,并添加了一些文本框,按钮等。现在我创建了另一个名为University.cs的.cs项目,并希望在University.cs中包含Default.aspx以使用文本框中的值。这是Default.aspx.cs的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}






protected void LoginAs_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void loginButton_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=d:\\CourseRegistration\\WebSite1\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";


    Int32 verify;
    string query1 = "Select count(*) from LoginTable where ID='" + idBox.Text + "' and Password='" + passwordBox.Text + "' and Type='" + LoginAs.SelectedValue + "'";
    SqlCommand cmd1 = new SqlCommand(query1, con);
    con.Open();
    verify = Convert.ToInt32(cmd1.ExecuteScalar());
    con.Close();
    if (verify > 0)
    {
        if (LoginAs.SelectedValue == "Administrator")
            Response.Redirect("Admin.aspx");
        else if (LoginAs.SelectedValue == "Student")
            Response.Redirect("Student.aspx");
        else if (LoginAs.SelectedValue == "Instructor")
            Response.Redirect("Instructor.aspx");
    }
    else
    {
        idBox.Text = "";
        LoginError.Visible = true;
    }

}
} 

I can use all textboxes, buttons etc. in this Default.aspx.cs file but they are not usable in University.cs file. I tried to write using Default.aspx.cs but it did not work. What can i do about this?

我可以使用此Default.aspx.cs文件中的所有文本框,按钮等,但它们在University.cs文件中不可用。我尝试使用Default.aspx.cs编写,但它不起作用。我该怎么办?

Thank you

谢谢

1 个解决方案

#1


1  

You cannot include a aspx class in a .cs class. You would instead have to try passing the data through some other means (eg: Session, application variable, storing the data in cookies, etc).

您不能在.cs类中包含aspx类。您将不得不尝试通过其他方式传递数据(例如:会话,应用程序变量,将数据存储在cookie中等)。

#1


1  

You cannot include a aspx class in a .cs class. You would instead have to try passing the data through some other means (eg: Session, application variable, storing the data in cookies, etc).

您不能在.cs类中包含aspx类。您将不得不尝试通过其他方式传递数据(例如:会话,应用程序变量,将数据存储在cookie中等)。