Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to findcontrol in repeater

I have created a repeater
i have added
C#
var lbl = new Label();
               lbl.ID = "lbl";

               var img = new Image();
               img.ID = "img";

in c# code and also

C#
RepeaterStrongestSkillsMax2.Controls.Add(img);
                RepeaterStrongestSkillsMax2.Controls.Add(lbl);


Now i am trying to foreach (var v in Repeater)
{
// Here i would like to find the controls that i have created(lbl and img)



}
Posted
Comments
ZurdoDev 19-Apr-13 15:29pm    
v.FindControl(imgid)
Kurac1 19-Apr-13 16:11pm    
hi yeah if they are on the same page but i want to find the label in another place and there i can not find (imgid) i think i should use something like Repeater.Items.FindControl("imgrid") but that dont work
ZurdoDev 19-Apr-13 16:26pm    
What do you mean same page? Are you trying to find something on a different page?
Kurac1 19-Apr-13 16:29pm    
they are not in same class
ZurdoDev 19-Apr-13 16:43pm    
That doesn't matter. Your code indicates you are adding it to the repeater as a child so Repeater.FindControl should work.

1 solution

C#
Label label= null;
foreach (RepeaterItem i in rpt.Items)
{
    label=(Label)(i.FindControl("lbl"));
}



Hope it will help you.Goodluck
 
Share this answer
 
Comments
Kurac1 22-Apr-13 7:35am    
this is my code private void SetSkillAndLevelMax3()
{
int count = 0;
foreach (var relevantSkillsStrongest in ListBoxRelevantSkillForMission.Items)
{

var lbl = new Label();
lbl.ID = "skill" + count++;

var img = new Image();
img.ID = "pie" + count++;

string valueTextBox = relevantSkillsStrongest.ToString();
int indexOf = valueTextBox.IndexOf("-", StringComparison.Ordinal);
string value = valueTextBox.Substring(0, indexOf - 1);
lbl.Text = value + " ";


string valueLevel = relevantSkillsStrongest.ToString();
int indexOff = valueLevel.IndexOf("-", StringComparison.Ordinal);
string levelValue = valueLevel.Substring(indexOff + 2);

if (levelValue == "Beginner")
{
img.ImageUrl = @"~/_layouts/images/Cv.Knowit/pie-1-4.png";

}
else if (levelValue == "Intermediate")
{
img.ImageUrl = @"~/_layouts/images/Cv.Knowit/pie-2-4.png";
}
else if (levelValue == "Advanced")
{
img.ImageUrl = @"~/_layouts/images/Cv.Knowit/pie-3-4.png";
}
else if (levelValue == "Expert")
{
img.ImageUrl = @"~/_layouts/images/Cv.Knowit/pie-4-4.png";
}

RepeaterStrongestSkillsMax2.Controls.Add(img);
RepeaterStrongestSkillsMax2.Controls.Add(lbl);
}

}
Kurac1 22-Apr-13 7:35am    
And i am trying to do this , but dont get i to work
for (int i = 0; i < RepeaterStrongestSkillsMax2.Items.Count; i++)
{
var skillName = RepeaterStrongestSkillsMax2.FindControl("skill" + i);
var pieLevel = RepeaterStrongestSkillsMax2.FindControl("pie" + i);

hTextWriter.Write(skillName);
hTextWriter.Write(pieLevel);
}
C@dER@j 23-Apr-13 0:23am    
Don't use for loop over here..User foreach for this..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900