Domanda

Yesterday I began to write C# in order to develop usercontrols for Umbraco and I ran across a well-known problem where I get the error "The * name does not exist in the current context." I found inumerous answers to that particular problem, as:

Fixing "name does not exist in the current context" errors in Web Developer Express

Label doesnt exist in the current context

http://forums.asp.net/t/1006588.aspx/1?The+name+xxx+Does+Not+Exist+in+the+Current+Context

...and so on and so forth.

None of these questions, however, did help me solve my problem and I'm still banging my head against a wall.

I then discovered that my tags and their controls get discovered by my *.apsx.cs file, as long as I don't put them within any non-asp:-tag!

I'm trying to make a custom login control with a custom template, for that I need the -tag. Furthermore I put this within a inside an LoginView control.

<asp:LoginView ID="MemberValidation" runat="server"> 
   <AnonymousTemplate>
     <asp:Login ID="MemberLogin" runat="server" OnLoggedIn="MemberLogin_OnLoggedIn">
        <LayoutTemplate>
           <asp:Literal ID="InformationMessage" runat="server"></asp:Literal>
        </LayoutTemplate>
   </AnonymousTemplate>
   <LoggedInTemplate>...</LoggedInTemplate>
</asp:LoginView>

My LiteralControl (and all the other LiteralControls I have - this is just a tiny example) is not registrered within the *.aspx.design.cs file automatically, hence, it is not accessible in my *.aspx.cs file, however if I remove the tag and the tag, my LiteralControl pops up.

However, I need the two tags to make the code function as I wish, so my question remains unsolved: What can I do? Is it a bug?

Thanks in advance, Brinck10

È stato utile?

Soluzione

If I'm understanding your problem correctly, the Literal control cannot be accessed directly by the .cs as long as it placed within the AnonymousTemplate or LayoutTemplate tags.

If that's correct, that's because anything within these tags are (as you've guessed it) templates and do not represent an actual runtime instance, and the compiler cannot guarantee that these templates will be shown (e.g. User is logged in, so the LoggedInTemplate is shown instead), and therefore does not expose these controls automatically to the .cs.

In fact, the only thing that's instantly visible to the .cs will be the LoginView control. So to access your controls within the templates, you will have to "find" them at runtime with code similar to the following:

if (!User.Identity.IsAuthenticated)
{
    Login memberLogin = (Login)this.MemberValidation.FindControl("MemberLogin");
    Literal informationMessage = (Literal)memberLogin.FindControl("InformationMessage");
    informationMessage.Text = "Hello World";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top