Reputation: 896
I am working on an ASP.NET VB.NET Web Application. I inherited a bunch of forms from another application we have in house. I'm running into a very strange problem when working on the Login page.
This is an abbreviated version of my code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
<tr>
<td>
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:login>
</td>
</tr>
</table>
</asp:Content>
I have a <span id="span1">
that is located inside my web form, within the Content
part of the page. I can easily access this in my CodeBehind, and do whatever I want to do with it. However, if i move that span and put it inside the <asp:login>
part of the page, it doesn't seem to recognize it, it won't let me access it in code behind, it gives me a squiggly blue line and says
span1 is not declared. It may inaccessible due to protection level
This bit is from the top of the webform in designer
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Default" MasterPageFile="~/Site.master" %>
This bit is from the Login.aspx.vb page
Partial Class _Default
Just to say it again, id="span1"
works perfectly fine where it is shown in the code above, but when I move it inside the I cannot reference it anymore. Since I'm talking about this issue, for that matter I cannot add any new controls inside because I am not able to reference any other controls in vb.net. (this form was pretty much copied from another project, everything works properly I'm just not sure why I'm having this strange issue)
I noticed that a lot of people have similar issues, but in my case I'm working with <asp:login>
and I'm really not sure how it's affecting my controls.
EDIT: <span id="Span1" runat="Server" style="Color: Red"></span>
Upvotes: 0
Views: 393
Reputation: 19963
As per my comments, and as requested by the OP...
You're hitting a problem with the naming container.
When the <span runat="server">
is outside of the <asp:Login><LayoutTemplate>
it exists as an object within the page, which you can reference directly.
As soon as it's moved within that <LayoutTemplate>
it becomes a child of the <asp:Login>
control instead.
So to access the control, you can use the following...
CType(Logon1.FindControl("span1"), HtmlGenericControl).InnerHtml = "hello"
The FindControl
will bring back an object, but it needs to be "boxed" into the correct type before you can access the InnerHtml
property
Upvotes: 1
Reputation: 35544
You need to use FindControl on the Login1 Control
HtmlGenericControl hgc = Login1.FindControl("Span1") as HtmlGenericControl;
hgc.InnerText = "Span Found";
VB
Dim hgc As HtmlGenericControl = CType(Login1.FindControl("Span1"),HtmlGenericControl)
hgc.InnerText = "Span Found"
Upvotes: 2
Reputation: 37830
to access a control on server side, you must include the "runat='server'" attribute on a tag. That's what tells .NET that any given control is supposed to be worked with on the server side as well as the front-end.
Do note that it will change the ID produced in the rendered HTML
<span id="span1" runat="server"></span>
Upvotes: 1