I'm getting
'Object reference not set to an instance of an object' error when I have
Dim lblEnterNew As System.Web.UI.WebControls.LabelIf DataGrid1.EditItemIndex > -1 Then
lblEnterNew.Visible = False
Else
lblEnterNew.Visible = True
End If
and my control in the page is
<asp:Label id="lblEnterNew" runat="server" class="dept"></asp:Label>
Any idea why I'm getting this error?Are you sure that the error occurs on the label? The label looks ok to me.
You are getting the error because you are dim-ing an instance of a label in a variable at the same time it is probably already dimmed in the hidden code-behind in the '#Region " Web Form Designer Generated Code " ' region. You would probably not get the error if you dimmed it wit the New statement "Dim lblEnterNew as New System.Web.UI.WebControls.Label" but then you would have problems referencing the actual control on the web page.
Try removing the "Dim lblEnterNew as System.Web.UI.WebControls.Label" line from the code as this should fix the problem and there should be a statement similiar to: "Protected Withevents lblEnterNew as Label" near the top of the code behind page already.
That worked. Should all of my controls be dimmed in the '#Region' but not variables?
All controls which you create in the design mode of Visual Studio are automatically dimed in the #Region section. You can define your own variables just after the class has started!
0 comments:
Post a Comment