簡體   English   中英

Page.Load上的ASP.NET包含文件

[英]ASP.NET include file on Page_Load

我創建了ASP.NET Intranet站點,該站點具有用於檢查每個頁面的用戶訪問級別的代碼。 將此代碼放在需要限制的每個頁面的Page_Load事件上。

以下代碼放置在只有管理員和餐飲人員才能訪問的每個頁面上:

    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();
        }

    }
}

以下代碼放置在只有管理員和市場營銷人員才能訪問的每個頁面上:

    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();

任何幫助將不勝感激,謝謝。

您需要在ASP.NET中使用頁面繼承。 以下鏈接將幫助您獲得相同的體驗。 頁面繼承

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM