문제

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;
도움이 되었습니까?

해결책

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

다른 팁

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"}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top