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.
Hello, i have tried with your solution but it doesn´t work how i want. I need that the text of the label will be the same in all the content pages that use the MasterPage and with this example i only obtain that the text appear when the content page is the
page where i change the text. if i go to another page, the text of the label in MasterPage changes to the original change. Do you understand me?
I use ASP.Net v.2.0, Visual Web Developer Express and Visual Basic.
Thank you very much.
hello, excuse me for my bad english. I have to develop a web site for a signature of my career. One of the features that the site must support is to allow to the teacher of the signature put in the header of MasterPage a differect quotation everyday. To do
this, there is a page (AddQuotation.aspx) in administrator area, where the teacher enter the quotation. With your solution i modify the text of the label,(where appear the quotation text) in code-behind of AddQuotation.aspx and the quotation appears in the
header of the MasterPage, but when i visit another page of the site (that uses the same MasterPage too) the quoatation disappears. The quotation is persistent only when the actual content page is AddQuotation. How i can that the quotation will be persistent
for all the pages of the site?? I hope i explained now better.
Thank you very much.
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
help: Connecting to a Microsoft Access database with ASP.NET.
Hi!, nothing, your last solution doesn´t work. It makes the same that the first option. I declare LabelText property in the code-behind of MasterPage and i create the Page_Load procedure in the same file. Finally i assign a text to the LabelText property in
the code-behind of an content page and it doesn´t work. When i go to another page, the text changes to de original message. There is no way to make the label text persistent? 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.
Thanks a lot.
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.
This sort of stuff is precisely why we have databases--they provide a location where we can store information and allow that information to be changed as often as we'd like.
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.
None
0 Points
20 Posts
Modify Master Page at runtime
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.
Thanks
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: Modify Master Page at runtime
May 01, 2006 12:00 PM|StrongTypes|LINK
You'll need to create a public property in the master page.
public string LabelText
{
get
{
EnsureChildControls();
return Label1.Text;
}
set
{
EnsureChildControls();
Label1.Text = value;
}
}
Then in the content page, add the following directive:
<%@ MasterType VirtualPath="~/PathToMaster/Master.master" %>
or
<%@ MasterType TypeName="MasterPageClass" %>
And in code-beside of the content page:
((MasterPageClassName)this.Master).LabelText = "Some string"
HTH,
Ryan
None
0 Points
20 Posts
Re: Modify Master Page at runtime
May 02, 2006 11:11 AM|ahappyplumber|LINK
I use ASP.Net v.2.0, Visual Web Developer Express and Visual Basic.
Thank you very much.
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: Modify Master Page at runtime
May 02, 2006 11:24 AM|StrongTypes|LINK
No. Please provide a more concise explanation of what you are trying to do.
Ryan
None
0 Points
20 Posts
Re: Modify Master Page at runtime
May 03, 2006 03:39 AM|ahappyplumber|LINK
Thank you very much.
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: Modify Master Page at runtime
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
{
get
{
return ViewState["LabelText"] as string;
}
set
{
ViewState["LabelText"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string labelText = ViewState["LabelText"] as string;
if (labelText != null)
{
Label1.Text = labelText;
}
}
HTH,
Ryan
Participant
1001 Points
7915 Posts
Re: Modify Master Page at runtime
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 help: Connecting to a Microsoft Access database with ASP.NET.
None
0 Points
20 Posts
Re: Modify Master Page at runtime
May 03, 2006 09:19 AM|ahappyplumber|LINK
Thanks a lot.
Participant
1001 Points
7915 Posts
Re: Modify Master Page at runtime
May 03, 2006 10:02 PM|SomeNewKid|LINK
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.