查找aspx页面文字 [英] Find literals in aspx page

查看:84
本文介绍了查找aspx页面文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网页上42字面及其ID喜欢LTR1,ltr2,...,ltr41,ltr42。我想改变自己的Text属性在for循环中。

I have 42 Literals on my web page and their IDs like ltr1,ltr2,...,ltr41,ltr42. I want to change their text property in a for loop.

hereis的HTML code:

hereis the html code:

<table class="calendar">
    <tr>
        <td>
            <div class="day">
                <asp:Literal ID="day1" runat="server">
                </asp:Literal></div>
        </td>
    </tr>
 </table>

C#

for(int i=1;i<43;i++)
{
    ("ltr"+i).Text="something"; //I don't know which method I must use, so wrote like this
}

我怎样才能做到这一点?

How can I do this?

推荐答案

当您使用母版页时的控制并没有从Page.FindControl()方法直接得到。所以你需要去1平深找从ASP控制:的ContentPlaceHolder的ID

when you are using master page that time your control didn't got directly from Page.FindControl() method. so you need to go 1 level deep to find control from asp:ContentPlaceHolder id's.

母版页:

<asp:ContentPlaceHolder ID="cphContent" runat="server" />

ASPX页面:

<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="server">
    <asp:Literal ID="day1" runat="server"></asp:Literal>
</asp:Content>

ASPX Page.cs:

var literals = Page.Master.FindControl("cphContent").Controls.OfType<Literal>();
foreach (Literal literal in literals)
{
     literal.Text = "Your Text";
}

这篇关于查找aspx页面文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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