Reputation: 1379
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 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?
Upvotes: 0
Views: 5132
Reputation: 2045
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.
Master Page:
<asp:ContentPlaceHolder ID="cphContent" runat="server" />
aspx Page :
<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";
}
Upvotes: 1
Reputation: 304
you can do this, wrap a at the top level like this:
<div id="Content" runat="Server">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td><asp:Literal ID="Literal1" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal2" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal3" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal4" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal5" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal6" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal7" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal8" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal9" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal10" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal11" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal12" runat="server"></asp:Literal></td>
<td><asp:Literal ID="Literal13" runat="server"></asp:Literal></td>
</tr>
</table>
</div>
and you can code like this :
for (int i = 1; i < 10; i++)
{
Literal li = (Literal)Content.FindControl("Literal"+i);
}
Upvotes: 0
Reputation: 4420
You need to do a recursive lookup of the controls on the page. By performance .net dont to this by default.
Read more about Recursive FindControl here
Upvotes: 0
Reputation: 223282
You may load all the literals in a collection using OfType
and later you can modify their text property.
var literals = this.Page.Controls.OfType<Literal>();
foreach (Literal literal in literals)
{
literal.Text = "Your Text";
}
EDIT:
Since you have control inside another control on the page, you need to do a nested search for controls. You can try the following extension methods. Taken from this post.
First create a class for extension method as:
public static class ExtensionMethod
{
public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
foreach (Control item in collection)
{
yield return item;
if (item.HasControls())
{
foreach (var subItem in item.Controls.FindAll())
{
yield return subItem;
}
}
}
}
public static IEnumerable<T> FindAll<T>(this ControlCollection collection) where T : Control
{
return collection.FindAll().OfType<T>();
}
}
Later you can use it like:
protected void Button1_Click(object sender, EventArgs e)
{
var literals = this.Controls.FindAll<Literal>();
foreach (Literal literal in literals)
{
literal.Text = "Your Text";
}
}
Upvotes: 4
Reputation: 33
How about the Page.FindControl() method?
for(int i=1;i<43;i++)
{
Literal li = (Literal)Page.FindControl("ltr"+i);
li.Text="something";
}
Upvotes: 1