Skip to content

Basepage class problems.

Mike Belcher

Asked Mar 8, 2009, 7:29 PM EDT2 answers
Share
LinkedInX

Explanation:

This application was handed to me to cleanup and have it run in Net 2.0 framework. The original application had no code-behind files but used the single file method like ASP classic with <%  and %>. C# re-usable functions where used in every page with a virtual include. One include that was done at the top of each page with this line <!-- #include virtual="includes/global.aspx" -->. This contained C# code. What I am trying to do is change this to code behind. I have created a basepage class.

The problem is I can't get things to work as they should. I am getting a few errors. This all used to be in the same single file and now I am trying to separate what used to be a virtual include global.aspx code into a baseclass and then have all the code behinds in the application use that when the page runs. Anyone have any ideas? I know I am missing something. The code for and example page like default.aspx and the basepage class are listed below.

 

 

 

Default.aspx:

Errors:
the name 'G_site' does not exist in the current context.

 

 

TypeScript
public partial
class 
_Default : 
BasePage
{
private 
void Page_Load(object 
sender, EventArgs e)
{

// 
==============================================================
LoadGlobals(sender, e);
string chk = (string)Session["loginchk"];
String loginType = "";
HttpCookie cookie = Request.Cookies["LoginType"];

if (null 
!= cookie)
{
loginType = cookie.Value.ToString();
}

if ((chk ==
"admin") || 
(loginType == "Administrator"))
{
Response.Redirect("admin.aspx",
true);
}



if ((chk ==
"power") || 
(loginType == "PowerUser"))
{
Response.Redirect("poweruser.aspx",
true);
}


if ((chk ==
"publisher") || 
(loginType == "Publisher"))
{
Response.Redirect("publisher.aspx",
true);
}


if (loginType ==
"Consortia")
{
Response.Redirect("Consortia.aspx",
true);
}


if (loginType ==
"Site")
{
Response.Redirect("Usage.aspx",
true);
}


if (string.Compare(G_site.site_id,
string.Empty) != 0)
------- ERROR = the name 
'G_site' does not exist in 
the current context.
{ // site
Response.Redirect("Usage.aspx");
}

else
{
Response.Redirect("Consortia.aspx");
}
// 
==============================================================



} // END protected void 
Page_Load(object sender, EventArgs e)

 

 

 

 

 

} //END public partial class _Default : BasePage







 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BasePage.cs:

 

 

PowerShell
public class
BasePage : 
System.Web.UI.Page
{

protected 
override 
void OnLoad(EventArgs e)
{
// ... add custom logic here ...


// Be sure to call the base class's 
OnLoad method!
base.OnLoad(e);
}







// BEGIN GLOBAL - The following was 
loaded with a virtual include  <!-- 
#include virtual="includes/global.aspx" --> 
 in the original application.
struct 
Site
{
public 
string site_id;
public 
string group_id;
public 
string consortia_id;
public 
string name;
public 
string publisher;
};

Site G_site;

string G_title;
string 
G_conn_string = "User 
ID=xxxxx;Password=xxxxxxxx;database=Net;server=york.xxxxx.com;Connection 
Timeout=60;";



public 
void LoadGlobals(Object 
sender, EventArgs e)
{
SqlConnection conAIP;
SqlCommand cmdSelect;
SqlDataReader dtrAccessControl;
SqlDataReader dtrConsortia;
SqlDataReader dtrSite;
SqlDataReader dtrGroup;
SqlDataReader dtrPublisher;
G_site.site_id = "";
G_site.name = "";
G_site.group_id = "";
G_site.publisher = "";
conAIP = new 
SqlConnection(G_conn_string);
try
{
conAIP.Open();
if 
(User.Identity.IsAuthenticated)
{
cmdSelect = new 
SqlCommand("select site_id from 
AIPSite_Access_Control where user_id = '" + 
User.Identity.Name + "'", 
conAIP);
dtrAccessControl = cmdSelect.ExecuteReader();
while 
(dtrAccessControl.Read())
{
G_site.site_id = dtrAccessControl["site_id"].ToString();
}
dtrAccessControl.Close();



if ((string.Compare(G_site.site_id,
string.Empty) != 0) 
&& (((string)Session["loginchk"]) 
!= "publisher"))

{ // site

PowerShell
cmdSelect = new 
SqlCommand("select site_name from AIPSites 
where site_id = '" + G_site.site_id + 
"'", conAIP);
dtrSite = cmdSelect.ExecuteReader();
while 
(dtrSite.Read())
{
G_site.name = dtrSite["site_name"].ToString();
}
dtrSite.Close();



cmdSelect = new 
SqlCommand("select group_id from 
AIPGroup_Members where site_id = '" + G_site.site_id +
"'", conAIP);
dtrGroup = cmdSelect.ExecuteReader();
while 
(dtrGroup.Read())
{
G_site.group_id = dtrGroup["group_id"].ToString();
}
dtrGroup.Close();
}

else
{ // consortia
cmdSelect = new 
SqlCommand("select consortium_id from 
AIPConsortia_Access_Control where user_id = '" + 
User.Identity.Name +
"'", conAIP);
dtrAccessControl = cmdSelect.ExecuteReader();
while 
(dtrAccessControl.Read())
{
G_site.consortia_id = dtrAccessControl["consortium_id"].ToString();
}
dtrAccessControl.Close();



cmdSelect = new 
SqlCommand("select consortium_name from 
AIPConsortia where consortium_id = '" + 
G_site.consortia_id + "'",
conAIP);
dtrConsortia = cmdSelect.ExecuteReader();
while 
(dtrConsortia.Read())
{
G_site.name = dtrConsortia["consortium_name"].ToString();
}
dtrConsortia.Close();


cmdSelect = new 
SqlCommand("select publisher from 
AIPPublisher_Access_Control where user_id = '" + 
User.Identity.Name +
"'", conAIP);
dtrPublisher = cmdSelect.ExecuteReader();
while 
(dtrPublisher.Read())
{
G_site.publisher = dtrPublisher["publisher"].ToString();
Response.Write("<!-- pub lookup:" 
+ G_site.publisher + " -->");
}
dtrPublisher.Close();


} // END Else 
((string.Compare(G_site.site_id, string.Empty)

} // END if (User.Identity.IsAuthenticated)

} // END try

 

HTML / XML
catch (Exception 
ex)
{
//Response.Write("<!-- exception: " + 
ex.Message + " -->");
}
finally
{
conAIP.Close();
}
Response.Write("<!-- user=" 
+ User.Identity.Name + ";publisher=" 
+ G_site.publisher + "; site_id=" 
+ G_site.site_id + " -->");

}
// END GLOBAL

 

 

 

 

 

 

} // END public class BasePage : System.Web.UI.Page

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    "G_site" isn't available to the derived class because it is private be default. If you want to make this available to the derived classes, you will have to change its scope in the base. Or provide an accessor method.

    Also, there are other ways to accomplish the idea of having some global data available to your classes/pages. Not advocating here, but you could use global.asax which would provide one place for you to keep this data, although I personally use this very sparingly.

    Putting it in a base class isn't bad, but each time a derived class is created it creates its own copy of that data in RAM, and takes time. Which without evaluating everything in the base could be good or could be horrible for performance.

    Hope this helps.
    0
  • 0
    How would i go about sticking this in a global.asax  file? Is there a place within the file where this code should go and  any other considerations?


    I made some changes to the lines below but  now get this error:

    ERROR:
    Inconsistent accessibility: field type 'BasePage.Site' is less accessible than field 'BasePage.G_site' 


    Gettting closer to it working.


      // BEGIN GLOBAL - The following was loaded with a virtual include in the original application.
       struct Site
      {
          public string site_id;
          public string group_id;
          public string consortia_id;
          public string name;
          public string publisher;
      };

       protected string G_title;
       protected string G_conn_string = "User ID=xxx;Password=xxx;database=NetxP;server=newxx.xx.com;Connection Timeout=60;";


       protected Site G_site;

     

    ERROR:
    Inconsistent accessibility: field type 'BasePage.Site' is less accessible than field 'BasePage.G_site'


    public Site G_site;

    I changed it to that but still getting the same error.

    :(





    How is the correct way to access this in a  code behind of a page?

    0