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>
<formid="form1"runat="server">
<div>
<asp:LoginViewID="LoginView1"runat="server">
<LoggedInTemplate>

<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>
<br/>
<asp:ButtonID="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;

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

本日志由 flyinweb 于 2011-07-15 09:07:37 发表,目前已经被浏览 3058 次,评论 2 次;

作者添加了以下标签: LoginViewTemplated Controls

引用通告:http://www.517sou.net/Article/632/Trackback.ashx

评论订阅:http://www.517sou.net/Article/632/Feeds.ashx

评论列表

  1. Gravatar
    蜡笔小清
    2011-07-16 00:16:38 | # | 回复
    http://www.2mysite.net/scriptencoder/screnc.asp
    站长你好,看到你有这个网页版加密的,比微软下载的screnc要方便些,不用到处都安装,呵呵。可以给源码我参考一下吗?我也是网站开发者,谢谢了,期待您的回复。我的邮箱是cs@mlmzj.com
  2. Gravatar
    flyinweb
    2011-07-17 11:52:40 | # | 回复
    @蜡笔小清 已经发送到你QQ邮箱
(必填)
(必填,不会被公开)