How can I access an object inside of an asp:LoginView

160 views Asked by At

I have a LoginView on a site master page that displays the username of the logged-in user. I modifies it to make the username a link to that user's settings, using a LinkButton.

I need to be able to conditionally enable or disable the LinkButton from within Page_Load. How do I get a reference to the LinkButton?

The LinkButton doesn't appear in the designer.cs file, but the LoginView does. I have tried looking at its controls property in the debugger and also tried using FindControl(LinkButton's ID) but that returns null.

--Jacob

2

There are 2 answers

1
parag On

Make sure that LinkButton is Asp.Net server control with "runat="server" attribute. If it is a server control then it should appear in your designer.cs file. Once it appeared in designer.cs file, you can access that control by its name or using FindControl method.

Some times Visual Studio IDE creates problem and it does not update desinger.cs file. Try to switch between designer view, mark up view and code view. It will update designer.cs file if the markup is correct.

0
wazz On

You can't use the LogIn "as is", you have to create a LayoutTemplate inside the control.

<asp:Login ID="LoginUser" runat="server">
    <%--the LayoutTemplate must include controls (with Text property) 
        with ID values UserName and Password--%>
    <LayoutTemplate>
        
        Your stuff here. Textboxes for user name and password, etc...

    </LayoutTemplate>
</asp:Login>

Then you can find a control by

Label myErrorLabel = (Label)LoginUser.FindControl("logInErrorDetails");