[quoted text, click to view] Chris wrote:
> Hi,
>
> I tried to create a class which must change the propety 'visible' of a
> <link> tag in the masterpage into 'false' when the user is logged. But i get
> the error: "Object reference not set to an instance of an object"
> for the line 'If mpg.FindControl("lkred").Visible = True Then'.
>
> I couldn't find sofar the solution.
> Any help would be appreciated ...
> Thanks
> Chris
>
> the class:
> ---------
> Imports Microsoft.VisualBasic
> Public Class loginkl
> Public Sub logkl()
> Dim pg As New Page
Here you are creating a completely new instance of the Page class. The
Page class is the base class for pages and doesn't contain any controls
at all.
[quoted text, click to view] > Dim mpg As MasterPage
> If pg.User.Identity.IsAuthenticated = True Then
> If mpg.FindControl("lkred").Visible = True Then
The FindControl method returns a null reference. As there are no
controls in the page object, the control you are looking for can of
course not be found.
[quoted text, click to view] > ....
> End If
> End Sub
> End Class
>
> code-behind:
> -----------
> Partial Class MasterPage
> Inherits System.Web.UI.MasterPage
> Protected Sub Page_Init(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Init
> Dim lg As New loginkl
> lg.logkl()
Here you should to pass a reference to the current Page into the method,
so that it can use that to locate the control.
Better yet, why not pass a reference to the control into the method.
That way you don't have to use FindControl to locate it, and the method
can be used to hide any control that you like, not only a control named
"lkred".
I am not sure, but the Init event of the master page might also occur
too early to access the controls of the page. If the markup of the page
has not yet been parsed, the page does not yet contain any controls.
Then you have to use an even that occurs later in the cycle.
[quoted text, click to view] > End Sub
> End Class
>
> masterpage.master:
> ------------------
> <link runat="server" id="lkred" href="App_Themes/red.css" rel=Stylesheet
> type="text/css" visible="true"/>
>
>
--
Göran Andersson
_____