Last post May 03, 2006 10:02 PM by SomeNewKid
May 01, 2006 11:45 AM|ahappyplumber|LINK
Hello, i´m developing a web site that uses MasterPages and i want to modify a Label sited at the top of my MasterPage. I have intended with the instruction Label1.Text="example" into one of the pages that uses the MasterPage and it works, but when i go to
another page the label text changes to the original text. How i have to do? i want that the text of the label will be the same in all site pages that use the MasterPage.
May 01, 2006 12:00 PM|StrongTypes|LINK
You'll need to create a public property in the master page.
public string LabelText
Label1.Text = value;
Then in the content page, add the following directive:
<%@ MasterType VirtualPath="~/PathToMaster/Master.master" %>
<%@ MasterType TypeName="MasterPageClass" %>
And in code-beside of the content page:
((MasterPageClassName)this.Master).LabelText = "Some string"
May 02, 2006 11:11 AM|ahappyplumber|LINK
May 02, 2006 11:24 AM|StrongTypes|LINK
Do you understand me?
No. Please provide a more concise explanation of what you are trying to do.
May 03, 2006 03:39 AM|ahappyplumber|LINK
May 03, 2006 03:59 AM|StrongTypes|LINK
You'll need to make it persist somehow. I'd put the value in ViewState on the master page.
public string LabelText
return ViewState["LabelText"] as string;
ViewState["LabelText"] = value;
protected void Page_Load(object sender, EventArgs e)
string labelText = ViewState["LabelText"] as string;
if (labelText != null)
Label1.Text = labelText;
May 03, 2006 04:27 AM|SomeNewKid|LINK
I think ahappyplumber is looking to have the updated Label apply for all users and to all pages. In this case, the LabelText property will, of course, need to use a database or text file, and not the page's ViewState. Perhaps the following tutorial will
Connecting to a Microsoft Access database with ASP.NET.
May 03, 2006 09:19 AM|ahappyplumber|LINK
May 03, 2006 10:02 PM|SomeNewKid|LINK
Must i use database or text file?? i think that use a database for this feature is some exaggerated, but i´m not an expert.
If the quotation on your website is the only the text that changes, then you are right that the database is unnecessary. However, you do still need to find somewhere that will accept text, and allow that text to be changed. You cannot use your code for
this. Using a text file is a super-simple way to allow some text on your website to be changed.
To learn how to read and write text files using .NET, please refer to the following tutorial:
Reading and Writing Text Files with the .NET Framework. The only trick here is that you need to update the security permissions of that text file, so that the ASPNET user
has write permissions. From My Computer on your desktop, navigate to the folder in which you have placed your quotation.txt file. Right-click on the file, and select Properties. Within the Properties window, select the Security tab. If the ASPNET user is not
already listed, you will need to add it. Then, give the ASPNET user "modify" permissions.