How to find a child control nested in div tags in ASP.NET
Hi i want to find an image button control which is in a table which is nested in 3 div tags actually it returns null if i use the below code
for (int j = 1; j < 38; j++)
{
string s = "ib_s" + j;
ImageButton img = (ImageButton)FindControl(s.ToString());
if (status[j] == "B")
{
img.ImageUrl = "~/graphics/Booked.jpg";
img.Enabled = false;
}
else
{
img.ImageUrl = "~/graphics/Available.jpg";
}
}
as i wanted to开发者_StackOverflow中文版 find image buttons with id's ib_s1, ib_s2 .... and change the image url.
And i use a master page for this page, so please help me.
As @Filip Ekberg commented, make sure
ImageButton
s haverunat="server"
attribute.If the
ImageButton
is on Content Page then you cannot find them on theMasterPage
's code behind.
Mark the outer div with runat="server" attribute and search for ImageButton in that div: ImageButton img = containerDivId.FindControl(imageButtonServerID) as ImageButton;
By the way, could you explain your aim? What you want to achieve on this page? As you have a table with 38 elements I suppose that it's would be better to use some data-driven control like Repeater here.
I got using this code from here http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
for (int j = 0; j < 37; j++)
{
string s = "ctl00$ContentPlaceHolder1$ib_s" + k;
ImageButton img = (ImageButton)FindControl(s.ToString());
if (status[j] == "B")
{
img.ImageUrl = "~/graphics/Booked.jpg";
img.Enabled = false;
}
else
{
img.ImageUrl = "~/graphics/Available.jpg";
}
k++;
}
精彩评论