Domanda

I have a loginview , that is in master page. and in login mode of login control, I have a hyperlink. I want access to hyperlink , but I get error(Object not refrence ...)

my code is :

HyperLink hp = FindControl("LoginView1_").FindControl("hpMng") as HyperLink;
È stato utile?

Soluzione

The FindControl method only searches for control that are under the collection of controls you looking for. It doesn't do a full recursive search.

And in your case, you can do something like this since you are on the same page (MasterPage)

        var h = this.login.FindControl("link") as HyperLink;

        this.msg.Text = h.Text;

Remember if the HyperLink control is in the LoggedInTemplate it will only be available when the user is logged in. If the HyperLink control is in the AnonymousTemplate the control will only be available when the user has not been logged in the application. Take this into consideration to avoid a null reference exception

Altri suggerimenti

For me, it works fine. You are trying after login? Obviously, the control will not be render if you have declared it inside de LoggedInTemplate and you are not logged.

?FindControl("HeadLoginView").FindControl("hpMng")
{Text = "aaa"}
    System.Web.UI.WebControls.HyperLink: {Text = "aaa"}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top