简体   繁体   English

如何从h1元素获取文本?

[英]How to get the text from h1 element?

I have a master and content page: 我有一个母版页和内容页:

1) On master page: 1)在母版页上:

Markup: 标记:

<h1 id="SeoActionH1" runat="server"></h1>

Code behind: 后面的代码:

public string SeoActionHeader1
{
    set
    {
        SeoActionH1.InnerText = string.IsNullOrEmpty(value) ? string.Empty : value;
    }
    get
    {
        return SeoActionH1.InnerText;
    }
}

2) On content page: 2)在内容页面上:

Code behind: 后面的代码:

private Main MainMasterPage
{
    get { return Master as Main; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string groupName = MainMasterPage.SeoActionHeader1;
    }
}

If for example the content of h1 is: Hello, I've got the following: 例如,如果h1的内容是:您好,我得到以下信息:

"\r\n                                "

What's going on? 这是怎么回事?

did you call this SeoActionHeader1 property on page load event? 您是否在页面加载事件中将此SeoActionHeader1属性称为?

or ty this way 还是这样

public string MyString{ get; set; }

public void Page_Load(object sender, EventArgs e)
{
    MyString = string.IsNullOrEmpty(value) ? string.Empty : value;
}

Then in your markup: 然后在您的标记中:

<h1><%= MyString %></h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM