Possible reasons for not being able to see a control from the codebehind?
I am looking at a Main.Master with a single <asp:Content>
control and inside that some ASP.NET login stuff (LoginView, LoginName, etc).
I tried adding a Label
like so:
<asp:LoginName ID="LoginName1" runat="server" FormatString="Welcome {0} <br />" />
<asp:Label ID="Label1" runat="server" />
but it doesn't show in the codebehind.
Any obvious reason for this?
Sometimes the Visual Studio Designer gets out of sync and does not generate the Designer code files correctly. These are some methods I've found helpful:
- Switch between HTML view and Design view.
- Make sure the project is compiled.
- Try to remove and reinsert the new control.
- Open and close the aspx file.
I haven't really found a pattern yet on when exactly Visual Studio fails to add new controls correctly to the .designer.cs/vb
file, but a random combination of the stuff metioned above usually helped.
Check to see if you have a line like this in your Main.master.designer.cs file (assuming you use C#)
protected global::System.Web.UI.WebControls.Label Label1;
if not - add it.
If your label is inside a LoginView then as a LoginView is a container you need to do LoginView1.FindControl("Label1") and then cast it a Label.
VB:
Dim label1 as Label = CType(LoginView1.FindControl("Label1"),Label)
C#:
Label label1 = (Label)LoginView1.FindControl("Label1")
精彩评论