Access info from a usercontrol

D

Dave

Hi all,

I am still fairly new to dot net, so go easy...

Anyway,

I have my main page. In this page is a usercontrol that
manages the security of the page.

I am authenticating a user against Content Management
Server, then checking locally if they have specific
rights to content on that page.

My class in the usercontrol is...


##########################################################
public class UserAuthenticationAndTracking :
System.Web.UI.UserControl
{

SqlConnection conn = new SqlConnection
(ConfigurationSettings.AppSettings["conn"]);


private bool UserApproved;

private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize
the page here

DataSet DS = new DataSet();

if (!IsPostBack)
{
if
(CmsHttpContext.Current.User != null)
{
try
{
conn.Open
();

SqlCommand UserData = new SqlCommand("select *
from extranetUserDetails where UserApproved = 1 and
UserName = @UserName", conn);

UserData.Parameters.Add("@UserName",
SqlDbType.NVarChar).Value =
CmsHttpContext.Current.User.ToString();

SqlDataReader readData = UserData.ExecuteReader();

if
(readData.HasRows)
{

userApproved = true;
}
else
{

userApproved = false;
}
Trace.Warn
("Authenticated", UserApproved.ToString());

}
finally
{
conn.Close
();
}
}
}

}

public bool userApproved
{
get
{
return UserApproved;
}

set
{
UserApproved = value;
}
}


##########################################################


In my page, (.aspx) I want to see if the user is approved.

I am trying...

##########################################################

if
(UserAuthenticationAndTracking.userApproved)
{
txtUserName.Text
+= " USER APPROVED";
}
else
{
txtUserName.Text
+= " USER NOT APPROVED";
}

##########################################################


but when I compile, I am getting...

C:\Inetpub\wwwroot\MyCMSApp\Templates\nqt.aspx.cs(37): An
object reference is required for the nonstatic field,
method, or
property 'MyCMSApp.CMSUserControls.UserAuthenticationAndTr
acking.userApproved'


Am I missing something???

Previously, I had tried to just access a public variable
in the usercontrol, but that didn't work either.

Also, may be of assistance, when I type
UserAuthenticationAndTracking, I do not get the property
in the intellisense.

What I need to do is to make the userApproved status to
any other user controls and the page. I don't know if I
am going about it in the right way, so any assistance
will be very much appreciated.

Best regards,
David Colliver.
http://www.DerbyFOCUS.com
 
J

John Saunders

Dave said:
Hi all,

I am still fairly new to dot net, so go easy...

Anyway,

I have my main page. In this page is a usercontrol that
manages the security of the page.

I am authenticating a user against Content Management
Server, then checking locally if they have specific
rights to content on that page.

My class in the usercontrol is...


##########################################################
public class UserAuthenticationAndTracking :
System.Web.UI.UserControl
....

public bool userApproved
{
get
{
return UserApproved;
}

set
{
UserApproved = value;
}
}


##########################################################


In my page, (.aspx) I want to see if the user is approved.

I am trying...

##########################################################

if
(UserAuthenticationAndTracking.userApproved)
....

but when I compile, I am getting...

C:\Inetpub\wwwroot\MyCMSApp\Templates\nqt.aspx.cs(37): An
object reference is required for the nonstatic field,
method, or
property 'MyCMSApp.CMSUserControls.UserAuthenticationAndTr
acking.userApproved'


Am I missing something???

Yes. :)

UserAuthenticationAndTracking is a class. That's different from being an
instance of a class, which is what you need.

If it's not already declared in the codebehind of your page, try declaring
this:

protected UserAuthenticationAndTracking userAuthenticationAndTracking;

You should then be able to access
"userAuthenticationAndTracking.userApproved".


BTW, the usual naming conventions have properties and methods declared with
a leading capital, so it should be UserApproved for the property, and
perhaps userApproved for the private variable. I personally would use
_userApproved for the private member.

Also BTW, good work on using a property. A lot of people don't figure that
one out. And I assume you already know about putting events inside of user
controls...


John Saunders
 
D

Dave

Hi John,

Thanks for your reply.

I have put the protected line into my .aspx, though in
the intellisense, the property still isn't appearing. Any
reason for that?

I am still getting used to the coding conventions, I am
mostly there, (camel case and that other one...).

Events in user controls??? I think I am doing this, but
not sure if I am doing it right. Can you supply a quick
example? (I am not sure if you mean events controlled by
components of a user control, or events controlled by
components of the page that hosts the user control)

In this particular case, this control probably won't need
an event, apart from the page load. All I am doing is
some basic authentication and tracking.

I have other problems at the minute as well, but is OT.
(The problem is that I am creating a DB connection,
querying into a datareader, then based on values in the
database, do other DB work, using the same connection. In
classic ASP, I can re-use the connection, but for some
reason, I can't in this case.)

Best regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com
 
D

Dave

I just had one of those D'oh!!! moments.

I hadn't noticed that you called the instance of the
class with a lower case U.

Regards,
Dave.
 
D

Dave

Still got a slight problem.

It now compiles, but I get...
Object reference not set to an instance of an object.
on the line where I try to use it (the .userApproved)

Regards,
Dave Colliver.
http://www.HullFOCUS.com
 
J

John Saunders

Dave said:
Hi John,

Thanks for your reply.

I have put the protected line into my .aspx, though in
the intellisense, the property still isn't appearing. Any
reason for that?

Sorry, I don't know.
I am still getting used to the coding conventions, I am
mostly there, (camel case and that other one...).

Events in user controls??? I think I am doing this, but
not sure if I am doing it right. Can you supply a quick
example? (I am not sure if you mean events controlled by
components of a user control, or events controlled by
components of the page that hosts the user control)

I meant an event generated by the user control. The idea would be that the
user control tells the page that something has happened, then the page can
use the properties of the user control to do something about it.

John Saunders
 
J

John Saunders

Dave said:
Still got a slight problem.

It now compiles, but I get...
Object reference not set to an instance of an object.
on the line where I try to use it (the .userApproved)

Dave, here's some code that's working for me in VS2003:

WebForm1.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UserControls
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected WebUserControl1 WebUserControl11;

private void Page_Load(object sender, System.EventArgs e)
{
WebUserControl11.SomeInteger += 1; // There _will_ be a
quiz...
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);

WebUserControl11.MyButtonClicked += new
EventHandler(WebUserControl11_MyButtonClicked);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void WebUserControl11_MyButtonClicked(object sender,
EventArgs e)
{
Label1.Text = "WebUserControl11.SomeInteger = " +
WebUserControl11.SomeInteger.ToString();
}
}
}

WebUserControl1.ascx.cs:

namespace UserControls
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for WebUserControl1.
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
private int _someInteger;

protected System.Web.UI.WebControls.Button Button1;

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public event EventHandler MyButtonClicked;

protected virtual void OnMyButtonClicked(EventArgs e)
{
if (MyButtonClicked != null)
{
MyButtonClicked(this, e);
}
}

public int SomeInteger
{
get {return _someInteger;}
set {_someInteger = value;}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void Button1_Click(object sender, System.EventArgs e)
{
OnMyButtonClicked(e); // or EventArgs.Empty, or new
EventArgs()
}
}
}

Give that a try and see if it works for you.

John Saunders
 
D

Dave

Hi John,

I have done this in the past and have the usercontrols
working.

In this instance, I am not sure what is going off. I am
wondering if it is something to do with the page
execution path.

At the moment, in the page, I have the page_load. In
here, I am checking the validity of the authentication.

In my user control, I have in the page_load, the database
check to get the user and the users parameters. On
checking the parameters for the user, I set the property
to true or false.

Back to the page, if I check the value in the property, I
am wondering if it is not known at this point, which if
the page page_load runs first, then that will probably be
true.

In my page, I have...


protected
MyCMSApp.CMSUserControls.UserAuthenticationAndTracking
userAuthenticationAndTracking;

private void Page_Load(object sender, System.EventArgs e)
{

Trace.Warn("PAGE:Approved",
userAuthenticationAndTracking.userApproved.ToString());


As I type the userAuthenticationAndTracking. I am getting
the intellisense showing me the options, of which
userApproved is one of them.

Thanks for your help so far.

Best regards,
Dave Colliver.
http://www.LiverpoolFOCUS.com
 
D

Dave

Hi again,

There is an added complication that I have just realised
(whilst doing a bit of research...)

I am breaking the page into bits, for templating.

I have a topbar, a leftbar and a rightbar. I am putting
these in usercontrols (like include files in classic asp)

This got me thinking, the top bar will ALWAYS be
consistent and ALWAYS in a page, so why shouldn't I put
my authentication user control in the top bar control.

so it is like...

Page
Top Bar Control
Authentication Control

The topbar control has nothing refrencing the user
control EXCEPT for what is in the ASPX
<%@ Register TagPrefix="uc1"
TagName="UserAuthenticationAndTracking"
Src="UserAuthenticationAndTracking.ascx" %>

I guess that my UC is working correctly, as I know it is
accessing the database. In this revised scenario, is
there anything else I need to do?

Thanks for your assistance and apologies for the
confusion.

Best regards,
Dave Colliver.
http://www.Burton-on-TrentFOCUS.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top