2011-07-10 17 views
0

嗨,我想找到一个图像按钮控件,它嵌套在3个div标签中,如果我使用下面的代码,它实际上会返回null如何在ASP.NET中找到嵌套在div标签中的子控件

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"; 
    } 
} 

因为我想找到id为ib_s1,ib_s2 ....的图像按钮,并更改图像url。

我使用这个页面的母版页,所以请帮助我。

+1

'(的ImageButton)的FindControl(s.ToString());'总是返回null?你所有的ImageButton上都有'runat =“server”吗? –

回答

0
  1. 由于@Filip Ekberg评论,确保ImageButton■找runat="server"属性。

  2. 如果ImageButton在内容页上,那么您在MasterPage的代码后面找不到它们。

0

马克外层的div与RUNAT = “server” 属性,并在该分区中搜索的ImageButton:ImageButton img = containerDivId.FindControl(imageButtonServerID) as ImageButton;

顺便说一句,你能解释一下你的目标是什么?你想在这个页面上实现什么?由于你有一个38个元素的表,我想在这里使用一些像Repeater这样的数据驱动控制会更好。