开发者

How to find a child control nested in div tags in ASP.NET

开发者 https://www.devze.com 2023-03-19 08:40 出处:网络
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

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.


  1. As @Filip Ekberg commented, make sure ImageButtons have runat="server" attribute.

  2. If the ImageButton is on Content Page then you cannot find them on the MasterPage'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++;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消