This is a migrated thread and some comments may be shown as answers.

[Solved] How to Create Class Library or Global procedure to eliminate Redundant/Duplicate Codes...

5 Answers 178 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 07 Oct 2009, 07:15 PM
Hi,

Environment: ASP.NET AJAX Q2 2009 SP1 with VStudio SP1.

 

I am creating a site with Master->Detail Content pages (about 4). Currently in each Content Page code behind file  Page_Prerender Event, it calls another Procedure ShowHideMasterMenu. Both are in the same code behind file. I am repeating the same below codes for all content pages because all does the exactly same thing.

 

My question is there any way I can put the codes of public void ShowHideMasterMenu() into a class library or into a Public Procedure  and call it from all content pages Page_Prerender event? Below code (public void ShowHideMasterMenu() ) works but they are redundant and repeating codes for each content page.

 

Any help or pointing to right direction will be appreciated.

 

Sincerely

 

GC_0620
__________________________________________________________________________________________________________

protected void Page_Prerender(object sender, EventArgs e)

    {

        ShowHideMasterMenu();

    }

 

    public void ShowHideMasterMenu()

    {

        if (Session["SessionEmployeeId"] == null)

        {

            Response.Redirect("SessionExpired.aspx");

 

        }

        Panel masterUserDetailsPanel = (Panel)Master.FindControl("UserDetailsPanel");

        masterUserDetailsPanel.Visible = true;

        Label masterLoginLabel = (Label)Master.FindControl("LoginLabel");

        masterLoginLabel.Text = Session["SessionUserFirstName"].ToString().Trim() + " " + Session["SessionUserLastName"].ToString().Trim();

        RadMenu mastermenu = (RadMenu)Master.FindControl("RadMenu1");

        mastermenu.FindItemByText("Login").Visible = false;

        mastermenu.FindItemByText("Logout").Visible = true;

        Label mpLabel = (Label)Master.FindControl("Lblislogged");

 

 

        if (mpLabel != null)

        {

            mpLabel.Text = "1";

 

        }

 

        if ((Session["SessionUserEmplCategory"].ToString().Trim() == "User" | Session["SessionUserEmplCategory"].ToString().Trim() == "Manager") & Session["SessionUserIsAdmin"].ToString().Trim() == "False") //Regular (Manager or User)

        {

 

            mastermenu.Items.Remove(mastermenu.Items[0]);

            mastermenu.Items.Remove(mastermenu.Items[1]);

 

        }

        else if (Session["SessionUserEmplCategory"].ToString().Trim() == "PR Manager" && Session["SessionUserIsAdmin"].ToString().Trim() == "False") //PR Manager

        {

            mastermenu.Items.Remove(mastermenu.Items[2]);

 

        }

        else if (Session["SessionUserEmplCategory"].ToString().Trim() == "PR Associate" && Session["SessionUserIsAdmin"].ToString().Trim() == "False"//PR Associate

        {

            mastermenu.Items.Remove(mastermenu.Items[2]);

            mastermenu.Items[0].Items.RemoveAt(1);

         }

         else if (Session["SessionUserEmplCategory"].ToString().Trim() == "Manager" & Session["SessionUserIsAdmin"].ToString().Trim() == "True") //Admin

        {

            mastermenu.Items.Remove(mastermenu.Items[0]);

         }

         else

        {

 

        }

 

    }

5 Answers, 1 is accepted

Sort by
0
Thomas Salt
Top achievements
Rank 1
answered on 07 Oct 2009, 08:23 PM
Try overriding the Render method in your master page.  It may be too late in the page life cycle, but give it a whirl.



0
gc_0620
Top achievements
Rank 1
answered on 08 Oct 2009, 01:40 AM
Thanks Thomas but I have no clue how to use Render method in master page.

All I want to create a Class Library or Global Procedure for the Codes that I coded in ShowHideMasterMenu. My intention is that each content page in their Page_Render event call that Class Libray or Global Procedure and execute it because each of those content pages Pre_Render events are the same (i.e. not to repeat the same codes in each content page). 

gc_0620
0
Thomas Salt
Top achievements
Rank 1
answered on 08 Oct 2009, 01:54 PM
My mistake.  You can just create a class, put the code in that class, and then reference that class and call the appropriate function in each pages pre render event.

here is an article. http://www.aspfree.com/c/a/C-Sharp/C-Sharp-Classes-Explained/

as for a reference..here is an example of the signature for the override event to put in a master page...
protected override void Render(HtmlTextWriter writer) {

MyBase.Render(writer); [NOTE: this is vb syntax]
}



0
gc_0620
Top achievements
Rank 1
answered on 08 Oct 2009, 09:21 PM

Thomas,
 
It did not work. I am getting below error message. Below is my code also.  I wounder if Telerik has some sample project or Tutorial for what I am trying to do.

Thanks
___________

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'

Source Error:

Line 26:     public static string ClassShow1HideMasterMenu()
Line 27:     {
Line 28:         if (Session["SessionEmployeeId"] == null)
Line 29:         {
Line 30:             Response.Redirect("logout.aspx");

____________

 

Master table Session assignment Event:

 

protected void LoginButton_Click(object sender, EventArgs e)

    {

          SqlConnection strConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["User_Login_Connection"].ToString());

 

            SqlCommand Cmd = new SqlCommand("SELECT [UserTablePrimaryKey], [Last Name], [First Name],  [UserId], [UserPassword], [Admin], [EmplActive], [EmplCategory] FROM [UserTable] "+

               "WHERE [UserId] = '" + UserIdTextBox.Text + "' And [UserPassword] = '" + userPasswordTextBox.Text + "'", strConn);

 

           strConn.Open();

 

               SqlDataReader objDR = Cmd.ExecuteReader();

                if (objDR.Read())

                {

                     if ((objDR["EmplActive"].ToString()) == "False")

                    {

                         RadAjaxPanel1.ResponseScripts.Add(string.Format("alert('Inactive employee, access denied...');"));

                         return;

                    }

 

                    else

                    {

                

                        Session["SessionEmployeeId"] = objDR["UserTablePrimaryKey"];

                        Session["SessionUserId"] = objDR["UserId"];

                        Session["SessionUserPassword"] = objDR["UserPassword"];

                        Session["SessionUserLastName"] = objDR["Last Name"];

                        Session["SessionUserFirstName"] = objDR["First Name"];

                        Session["SessionUserIsAdmin"] = objDR["Admin"];

                        Session["SessionUserEmplCategory"] = objDR["EmplCategory"];

                       

                        Lblislogged.Text = "1";

                        RadMenu1.FindItemByText("Login").Visible = false;

                        RadMenu1.FindItemByText("Logout").Visible = true;

                        UserDetailsPanel.Visible = true;

                        LoginLabel.Text = Session["SessionUserFirstName"].ToString().Trim() + " " + Session["SessionUserLastName"].ToString().Trim();

 

           }

 

       }

    }

 

 

 

 

Content Page:

 

<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"  AutoEventWireup="true"

    CodeFile="Jobs.aspx.cs" Inherits="Jobs" %>

    <%@ MasterType VirtualPath="~/MasterPage.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

 

      ‘ ‘ ‘ ‘ ‘ ‘

      ‘ ‘ ‘ ‘ ‘ ‘

</asp:Content>

 

 

Content Page code behind File:

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Telerik.Web.UI;

 

public partial class Jobs: System.Web.UI.Page

 

 

{

 

Content Page Page_Init Event:

   

protected void Page_Init(object sender, EventArgs e)

    {

        showhellow.Text = PaClass.PaSayHellow(); //this works

 

        PaClass.ClassShow1HideMasterMenu();     // this don’t work.

 

     

    }

        ‘ ‘ ‘ ‘ ‘ ‘

      ‘ ‘ ‘ ‘ ‘ ‘

 

}

 

____________

Class PAClass Code:


 

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI;

using System.IO;

using System.Web;

using System.Web.Security;

using System.Web.UI.WebControls;

using Telerik.Web.UI;

using System.Reflection;

using System.Configuration;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

/// <summary>

/// Summary description for PaClass

/// </summary>

public class PaClass : System.Web.UI.Page

{

   

   public static string PaSayHellow()

    {

        return "Hello World Say Hellow!"//this works

 

    }

 

 

 

    public static string ClassShow1HideMasterMenu()

    {

        if (Session["SessionEmployeeId"] == null)

        {

            Response.Redirect("SessionExpired.aspx");

 

        }

        Panel masterUserDetailsPanel = (Panel)Master.FindControl("UserDetailsPanel");

        masterUserDetailsPanel.Visible = true;

        Label masterLoginLabel = (Label)Master.FindControl("LoginLabel");

        masterLoginLabel.Text = Session["SessionUserFirstName"].ToString().Trim() + " " + Session["SessionUserLastName"].ToString().Trim();

        RadMenu mastermenu = (RadMenu)Master.FindControl("RadMenu1");

        mastermenu.FindItemByText("Login").Visible = false;

        mastermenu.FindItemByText("Logout").Visible = true;

        Label mpLabel = (Label)Master.FindControl("Lblislogged");

 

 

        if (mpLabel != null)

        {

            mpLabel.Text = "1";

 

        }

 

        if ((Session["SessionUserEmplCategory"].ToString().Trim() == "User" | Session["SessionUserEmplCategory"].ToString().Trim() == "Manager") & Session["SessionUserIsAdmin"].ToString().Trim() == "False") //Regular (Manager or User)

        {

 

            mastermenu.Items.Remove(mastermenu.Items[0]);

            mastermenu.Items.Remove(mastermenu.Items[1]);

 

        }

        else if (Session["SessionUserEmplCategory"].ToString().Trim() == "PR Manager" && Session["SessionUserIsAdmin"].ToString().Trim() == "False") //PR Manager

        {

            mastermenu.Items.Remove(mastermenu.Items[2]);

 

        }

        else if (Session["SessionUserEmplCategory"].ToString().Trim() == "PR Associate" && Session["SessionUserIsAdmin"].ToString().Trim() == "False"//PR Associate

        {

            mastermenu.Items.Remove(mastermenu.Items[2]);

            mastermenu.Items[0].Items.RemoveAt(1);

         }

         else if (Session["SessionUserEmplCategory"].ToString().Trim() == "Manager" & Session["SessionUserIsAdmin"].ToString().Trim() == "True") //Admin

        {

            mastermenu.Items.Remove(mastermenu.Items[0]);

         }

         else

        {

 

        }

 

 

    }

   

}

 

 

0
Thomas Salt
Top achievements
Rank 1
answered on 09 Oct 2009, 02:56 PM
Try changing Sessions to HttpContext.Current.Session

I'm not sure why you have the system.web.ui.page extension..
public class PaClass : System.Web.UI.Page

{

 public static string PaSayHellow()   

    {

        return "Hello World Say Hellow!"//this works

    }

 


Try changing it just to and then referencing your static procedure
public class PaClass

{

 


Tags
Grid
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Thomas Salt
Top achievements
Rank 1
gc_0620
Top achievements
Rank 1
Share this question
or