I ...">

Filling multiple literals with the same value

I have several literals per page. For example,

<asp:Literal id="Label1" runat="server" />

I have about 10 of them on one page and you want to fill them with all the same values. Is there a good way to do this without reference to an identifier for each control?

+3
source share
5 answers

Create a public property that contains the value you want to display, and using aspx

<%= propertyName %>

where you want to display the value.

+11
source

If you use resource files, you can set the meta: resourcekey attribute to the same value for all 10 of them.

0
source

, , - id (, Label1 - Label10) . !

-1

(, ) ? , , , .

, , , . .

-1

What are your tags called. If they have a general naming convention, such as Label1 - Label10, you can do

for(int i = 1; i <= 10; i++)
{
    Literal l = Page.FindControl(string.Format("Label{0}", i)) as Literal
    if(l != null)
        l.Text = "Whatever";
} 

If they are not named the same way, you can simply insert the label names into the array and skip them, instead of having 10 explicit statements .Text =. eg.

string [] litNames = new string [] { "litFirst", "otherLit", "someOtherVar" }

foreach(string s in litNames)
{
    Literal l = Page.FindControl(s) as Literal
    if(l != null)
        l.Text = "Whatever";
}
-1
source

Source: https://habr.com/ru/post/1708205/


All Articles