简体   繁体   English

如何在 ASP.NET 中找到嵌套在 div 标签中的子控件

[英]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.我为此页面使用了母版页,所以请帮助我。

  1. As @Filip Ekberg commented, make sure ImageButton s have runat="server" attribute.正如@Filip Ekberg评论的那样,确保ImageButton具有runat="server"属性。

  2. 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.

相关问题 在asp.net中的嵌套转发器控件中找到单选按钮控件? - Find radio button control in nested repeater control in asp.net? 如何使用asp.net中div标签中的任何其他控件实现嵌套GridView功能 - How to achieve Nested GridView functionality using any other controls in div tags in asp.net Asp.net中的子控件 - Child Control in Asp.net 如何在ASP.NET LoginView中找到控件? - How to find a control in ASP.NET LoginView? 如何在ASP.NET中递归地查找控件 - How to find a control recursively in ASP.NET 在ASP.NET中,如何找到嵌套在DetailsView中然后嵌套在AJAX UpdatePanel控件中的TextBox的控件ID? - In ASP.NET how do I find the Control ID of a TextBox that is nested within a DetailsView which is then nested in an AJAX UpdatePanel control? 如何阅读asp.net服务器控件的标签之间的内容 - How to read content between tags of asp.net server control 如何在服务器控件属性中使用ASP.NET &lt;%=标记? - How to use ASP.NET <%= tags in server control attributes? 找到控制Asp.net - Find Control Asp.net 如何在ASP.NET用户或服务器控件上定义属性以允许多个字符串值作为具有自定义标记名称的嵌套标记? - How can I define a property on an ASP.NET user or server control to allow multiple string values as nested tags with a custom tag name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM