[英]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嗨,我想在嵌套在 3 个 div 标签中的表格中找到一个图像按钮控件,如果我使用下面的代码,它实际上返回 null
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 find image buttons with id's ib_s1, ib_s2.... and change the image url.因为我想找到 ID 为 ib_s1、ib_s2 的图像按钮......并更改图像 url。
And i use a master page for this page, so please help me.我为此页面使用了母版页,所以请帮助我。
As @Filip Ekberg commented, make sure ImageButton
s have runat="server"
attribute.正如@Filip Ekberg评论的那样,确保ImageButton
具有runat="server"
属性。
If the ImageButton
is on Content Page then you cannot find them on the MasterPage
's code behind.如果ImageButton
在 Content Page 上,那么您无法在MasterPage
的代码后面找到它们。
Mark the outer div with runat="server" attribute and search for ImageButton in that div: ImageButton img = containerDivId.FindControl(imageButtonServerID) as ImageButton;
用 runat="server" 属性标记外部 div 并在该 div 中搜索 ImageButton: 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.由于您有一个包含 38 个元素的表,我想在这里使用像 Repeater 这样的数据驱动控件会更好。
I got using this code from here http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs我从这里开始使用此代码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++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.