Amit Kumar
Amit Kumar

Reputation: 5962

unable to find the hyperlink of parent mater page

This hierarchy of master page

|--main.master
   |-- index.aspx
   |-- user.master
       |-- login.aspx

means master page of user.master is main.master, master page of login page is user.master

I have a loginview control on main.master page.

Code for loginview;

<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
   <AnonymousTemplate>
      [ <a href="~/PageCommon/login.aspx" id="HeadLoginStatus" runat="server" color="#87cfe6">Log In</a> ]
   </AnonymousTemplate>
   <LoggedInTemplate>
      Welcome <span class="bold">
      <asp:HyperLink ID="HyperLink1" ForeColor="PeachPuff" Font-Size="Larger" ToolTip="MyHome"  runat="server"> <asp:LoginName ID="HeadLoginName" runat="server" /></asp:HyperLink>
      </span>&nbsp;&nbsp;
      [
       <asp:LoginStatus ID="HeadLoginStatus" ForeColor="#87cfe6" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/PageCommon/index.aspx" />
      ]
   </LoggedInTemplate>
</asp:LoginView>

Now in code behind of login page I want to access the hyperlink of login view to change the navigate url of that hyperlink. But I am unable to access the hyperlink.

I used this code to access the hyperlink

if (User.Identity.IsAuthenticated)
{
   HyperLink rdirect = (HyperLink)Master.FindControl("HyperLink1");

   int index = utype.SelectedIndex;

   if (index == 0)
   {
      rdirect.NavigateUrl = ResolveUrl("~/PageUser/MyProfile.aspx");
   }
   else if (index == 1)
   {
      rdirect.NavigateUrl = ResolveUrl("~/PageAdmin/MyProfile.aspx");
   }
}

Master page of login page is user.master, and master page for user.master is main.master.

Upvotes: 0

Views: 205

Answers (2)

Tami
Tami

Reputation: 586

first of all to access any control in master page(which is inside ContentPlaceHolder) one way is, first access the ContentPlaceHolder than in this CPH(ContentPlaceHolder) find you desired control.(if its on root i.e outsede CPH than access it without finding CPH)

Case if LoginView is on root of Main.master:

 LoginView logInView = (LoginView)this.Master.Master.FindControl("HeadLoginView");
 HyperLink hyp2 = (HyperLink)logInView.FindControl("HyperLink1");

Case if LoginView inside ContentPlaceHolder of Main.master:

In Your scenario you have nested master pages..so if you define like this: asuming that in you main.master their is only one ContentPlaceHolder Id = ContentPlaceHolder1

ContentPlaceHolder User_cph1 = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1"); 

It will give you the User.master ContentPlaceHolder as their is only one CPH in masterpage(its assumption)

so one way is to define new contentplaceholde("ContentPlaceHolder2") in main.master and put you LoginView Control in it, than use this code :(purpose of defining new CPH is only to validate that this CPH not being used by user.master)

ContentPlaceHolder Main_cph2 = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder2");
        LoginView logInView = (LoginView)Main_cph2.FindControl("HeadLoginView");
        HyperLink hyp2 = (HyperLink)logInView.FindControl("HyperLink1");

it will first find CPH in Main.Master

than Find HeadLoginView in CPH

than find HyperLink in HeadLoginView

Hope it is some cleared to you :)

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76717

It is because you try to access a control of a master page of a master page.

This:

(HyperLink)Master.FindControl("HyperLink1");

does not work because you try to get the control from the master page, but the control is in the master page of the master page. I think you should have a property in the code-behind of user.master which returns this control and you should use that property in the code-behind of login.aspx.

Upvotes: 0

Related Questions