1 Replies - 4418 Views - Last Post: 23 November 2009 - 08:07 AM Rate Topic: -----

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

[Solved] Use Master page button in code on content page?

Post icon  Posted 22 November 2009 - 11:06 AM

Hi!

To simplify a little, this is what my problem is:
I have a Masterpage that holds a button (defined on the master page) that I want to appear on every page on the site. This button should be enabled if the user is logged in and disabled if the user is not logged in, i.e. "my account" that does stuff for a logged in user.

I've tried two ways:
1) User info in the master page
On a content page one can use the function User.Identity.IsAuthenticated which returns true if the user is logged in. This can not be used on the master page because "User" is not defined. Error correction options from the IDE says I should import "Microsoft.VisualBasic.ApplicationServices", doing so just gives a new error since "Identity" is not a member of the import.
2) Accessing the maste page button from a content page.
On the master page the button is named btnMyAccount. I would like to do somthing like MasterPage.btnMyAccount.Enabled = User.Identity.IsAuthenticated on the content page but that does not work.

Any ideas? :blink:

Regards
/Jens

This post has been edited by jens: 23 November 2009 - 08:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved] Use Master page button in code on content page?

#2 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: [Solved] Use Master page button in code on content page?

Posted 23 November 2009 - 08:07 AM

Ok, I found a solution, I've posted it as a tutorial named "Accessing MasterPage.master controls from a content page" which will hopefully appear soon in the ASP.NET tutorial area. Here is the short version:

The MasterPage.master
Partial Class MasterPage
	Inherits System.Web.UI.MasterPage

	Public Property tBox() As String
		Get
			Return txtBox1.Text
		End Get
		Set(ByVal value As String)
			txtBox1.Text = value
		End Set
	End Property

	Public Property btnState() As Boolean
		Get
			Return btnTest.Enabled
		End Get
		Set(ByVal value As Boolean)
			btnTest.Enabled = value
		End Set
	End Property

	Public Sub setBtn(ByVal state As Boolean)
		btnTest.Enabled = state
	End Sub
End Class



The content page Default.aspx
Partial Class _Default
	Inherits System.Web.UI.Page

	Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mp As MasterPage = CType(Me.Master, MasterPage)
		mp.btnState = Not mp.btnState
		mp.tBox = IIf(CheckBox1.Checked, "Checked", "UnChecked")
	End Sub

	Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
		Dim mp As MasterPage = CType(Me.Master, MasterPage)
		mp.setBtn(CheckBox1.Checked)
	End Sub
End Class



/Jens
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1