Page_Load上的ASP.NET包含文件

本文关键字:NET 包含 文件 ASP 上的 Load Page | 更新日期: 2023-09-27 18:19:50

我创建了ASP.NET intranet站点,该站点有一个代码来检查每个页面的用户访问级别。此代码放置在需要限制的每个页面的Page_Load事件上。

下面的代码被放置在只有Admin&餐饮人员应能够访问:

    public partial class cat_suppliers : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_employee where emp_uname = @UserName", con);
            cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["emp_role"].ToString() == "1")
                {
                    lblmessage.Text = "Admin";
                }
                else if (dr["emp_role"].ToString() == "3")
                {
                    lblmessage.Text = "Catering";
                }
                else
                {
                    Response.Write("<script>alert('Only Catering staff are authorized to access this page!!!')</script>");
                    Response.Write("<script>window.location.href='default.aspx';</script>");
                }
            }
            con.Close();
        }
    }
}

下面的代码被放置在只有Admin&营销人员应能够访问:

    public partial class mar_contractors : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_employee where emp_uname = @UserName", con);
            cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["emp_role"].ToString() == "1")
                {
                    lblmessage.Text = "Admin";
                }
                else if (dr["emp_role"].ToString() == "2")
                {
                    lblmessage.Text = "Marketing";
                }
                else
                {
                    Response.Write("<script>alert('Only Marketing staff are authorized to access this page!!!')</script>");
                    Response.Write("<script>window.location.href='default.aspx';</script>");
                }
            }
            con.Close();
        }
    }
}

我是否可以将公共代码放在单独的文件中,并将其包含在Page_Load事件中?如以下餐饮页面所示。

        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_employee where emp_uname = @UserName", con);
        cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            if (dr["emp_role"].ToString() == "1")
            {
                lblmessage.Text = "Admin";
            }
            else if (dr["emp_role"].ToString() == "3")
            {
                lblmessage.Text = "Catering";
            }
            else
            {
                Response.Write("<script>alert('Only Catering staff are authorized to access this page!!!')</script>");
                Response.Write("<script>window.location.href='default.aspx';</script>");
            }
        }
        con.Close();

任何帮助都将不胜感激,谢谢。

Page_Load上的ASP.NET包含文件

您需要在ASP.NET中使用页面继承。以下链接将帮助您获得同样的效果。页面继承