Wednesday 20 July 2011

Access LoginView Templated Controls with ASP 4.0 and C#


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. SS1.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.

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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:
Label1.Text = "Hello World";

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

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:
((Label)LoginView1.FindControl("Label1")).Text = "Hello World";

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:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LoginView ID="LoginView1" runat="server">
            <LoggedInTemplate>
                
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                
            </LoggedInTemplate>
        </asp:LoginView>
    </div>
    </form>
</body>

The Default.aspx.cs codebehind looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //How we would access the control outside of a template
        //Label1.Text = "Hello World";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //How we have to access it inside of the template
        ((Label)LoginView1.FindControl("Label1")).Text = "Hello World";
    }
}

No comments:

Post a Comment