This tutorial we will demonstrate how to access ASP.NET controls located in the LoggedInTemplate of a LoginView Control from the C# code behind class.

Adding the LoginView Control

At this point in the tutorial I have created a new ASP.NET Empty Web Site and have added in a new blank Web Form named Default.aspx. What we need to do now is add a LoginView Control to the Web Form. To do this open up Default.aspx to Design mode and:

  1. Expand the Login tab in your toolbox.
  2. Drag and drop a LoginView Control on to the Web Form.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

Now that we have added the LoginView Control, we have the option of adding ASP.NET controls in either the AnonymousTemplate or the LoggedInTemplate appropriately. Either way, when you add a control to a template it goes out of the scope of our Default.aspx.cs code behind class. Let’s add some controls to the LoggedInTemplate and see what happens when we try to access them in C# from our code behind class. To do this:

  1. Expand the LoginView tasks menu.
  2. SS2.gif

  3. In the Views DropDownList, select LoggedInTemplate.
  4. Drag and drop a Label into the editable area of the LoginView Control.
  5. Drag and drop a Button into the editable area of the LoginView Control under the Label.

Accessing Our Control

Right now we will leave the names of these controls default, Label1 and Button1. Normally, when you drop something onto a Web Form you can access the controls directly from the code behind class. However, with the controls in a template, this is not the case. To test this out open up the Default.aspx.cs code behind file and add in the following code to the Page_Load event method:

Notice, that intellisense immediately underlines Label1 and gives us an error that says ‘The name ‘Label1’ does not exist in the current context’.

This is a problem if we need to access this Label with C#. The way around this is to access it from the LoginView Control that we added earlier. To do this, get rid of the code we added earlier and open the Default.aspx back up to Design mode. Then double click the button we added earlier to generate a Click event method on that button. Then add the following code to that method:

If you’re looking for a really good web host, try Server Intellect – we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
What this will do, is call the FindControl method of the LoginView and find the Label we added named Label1. Then, we will cast that as a Label type so that we can access the Text property of it and change that to ‘Hello World’. Go ahead and load up the website and click the button to make sure that it works.

The Default.aspx source looks like this:

The Default.aspx.cs codebehind looks like this:

Download Source Files