更改母版页中按钮的文本 [英] change the text of button in master page

查看:50
本文介绍了更改母版页中按钮的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试从aspx页面更改母版页中按钮的文本。



我将以下内容放在母版页中:



Hi,

I am trying to change the text of a button in master page from an aspx page.

I put the following in the master page:

public LinkButton LButton
    {
        get { return LButton; }
        set { LButton = value; }
    }





然后我在.aspx页面中添加了以下内容:





then i added the following in the .aspx page:

<%@ MasterType VirtualPath="Site.Master" %>





最后在我的背后的代码中:





lastly in the code behind i put:

Master.LButton.Text = "logout";

< br $>




i得到错误消息:



用户代码的stackoverflow异常!它指向:








i get an error that says:

stackoverflow exception by user code! and it is pointing at:


public LinkButton LButton
    {
        get { return LButton; }
        set { LButton = value; }
    }





作为循环!



please帮助......



as a loop!

please help...

推荐答案

你的获取正在返回LButton,所以将进入相同的get等等。如果你有一个LinkBut​​ton,你试图返回,那么你的财产必须被称为LButton以外的东西。



Your "get" is returning LButton, so will go into the same get and so on. If you have a LinkButton you're trying to return then your property has to be called something other than LButton.

<asp:LinkButton ID="MyLinkButton" runat="server" />







public LButton {get {return MyLinkButton;} }





或者,如果您只想在母版页上创建一个名为LButton的属性,您将在其他地方使用





Or if you just want to create a property called LButton on the master page you will use elsewhere just use

public LinkButton {get;set;}





但它是最好的做法是不公开这样的控件,而是设置要更改的值的属性,因此要有一个名为ButtonLabel的字符串属性,并且在设置属性时主页面会更新LinkBut​​ton。



However it is best practise to not expose controls like this but to set up properties for the values you want to change, so have a string property called "ButtonLabel" or something, and the master page updates the LinkButton when the property is set.


我找到了一个解决方案...



我只在.aspx代码中添加以下内容:



I found a solution...

I only put the following in the .aspx code:

Button l1 = Page.Master.FindControl("Button1") as Button;
        l1.Text = "logout"; 







并且有效:)




and it works :)


这篇关于更改母版页中按钮的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆