Reputation: 1239
I have a simple asp:Label control on a web page. In the Page_Load event I am conditionally populating the label with a warning. Simple, right? This used to work but now I get an error saying the control is null.
The control declaration:
<table width="100%">
<tr>
<td align="right">
<asp:Label ID="lblWarningLabel" runat="server" Enabled="False"></asp:Label>
</td>
</tr>
</table>
The code-behind where lblWarningLabel is found to be null:
if (sServerName.Contains("Staging") || sServerName.Contains("Test"))
{
Int32 iIndex = sServerName.IndexOf("/");
lblWarningLabel.Text = "Warning: You are working on the " + sServerName.Substring(iIndex + 1) + " server!";
lblWarningLabel.CssClass = "WarningLabel";
lblWarningLabel.Enabled = true;
}
I have researched this and I have found some posters saying that their user control was null or that they were trying to create the control dynamically, neither of which pertain here. I am not using Resharper but since the last time I looked at this program, I have installed some Productivity Tools via NuGet.
Thanks for your help.
Update: I should add that another asp:Label control on the same page is working as expected. I also changed "False" to "false" and then removed the Enabled clause all together, but there was no change in behavior.
Upvotes: 1
Views: 2633
Reputation: 1239
I found a solution although I don't claim it's the best one:
I copied the page's HTML and code-behind and saved them, deleted the page from the project and recreated it, using the code I had saved. Re-built, re-tested, voila! It works now.
Upvotes: 1
Reputation: 4503
In your solution, that page should have a .designer.cs file. So if the page is Login.aspx, there should be a Login.cs and Login.designer.cs. Check the designer and see if the control is listed there, if not try to remove it from the .aspx page and add it back.
Upvotes: 0
Reputation: 13765
Is your control within a repeater now and it wasn't before? If that's the case there's a different way to access it:
Cannot Find Label Control In Repeater Control
might get you started.
It might actually be simpler than that however, sometimes the designer file created with your code front and code behind gets out of sync. You can try deleting the designer file, right clicking the aspx, and "convert to web application" to regenerate the designer file. Additionally clean/rebuild and closing and reopening VS sometimes correct the issue - if it is what I think it is.
Here's more info on that How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?
Upvotes: 0