asp.net runat div 中找到控制权

asp.net find control inside runat div

提问人:Gabriele Cozzolino 提问时间:10/4/2023 最后编辑:Gabriele Cozzolino 更新时间:10/6/2023 访问量:38

问:

我的标记中有这个div

<form id="form1" runat="server">
    <div class="mt-5">
        <div id="d" runat="server"></div>
    </div>
</form>

然后,我通过代码隐藏向其添加一些html元素:

string[] parole = { "morning", "good", "StackOverflow!" };
string[] ordinalID = { "2", "1", "3" };
string[] images = { "images/img-1.jpg", "images/img-2.jpg", "images/img-3.jpg" };

HtmlGenericControl unorderedList = new HtmlGenericControl("ul");
unorderedList.Attributes["class"] = "sortable-list";
unorderedList.Attributes["id"] = "sortable";
unorderedList.ClientIDMode = ClientIDMode.Static;


Int16 i = -1;
foreach (string parola in parole)
{
    ++i;
   
    HtmlGenericControl listItem = new HtmlGenericControl("li");
    listItem.Attributes["class"] = "item";
    listItem.Attributes["id"] = ordinalID[i];
    listItem.Attributes["draggable"] = "true";
    listItem.ClientIDMode = ClientIDMode.Static;

    HtmlGenericControl div = new HtmlGenericControl("div");
    div.Attributes["class"] = "details";

    HtmlGenericControl img = new HtmlGenericControl("img");
    img.Attributes["src"] = images[i];

    HtmlGenericControl span = new HtmlGenericControl("span");
    span.InnerText = parola;

    HtmlGenericControl info = new HtmlGenericControl("i");
    info.Attributes["class"] = "uil uil-draggabledots";

    div.Controls.Add(img);
    div.Controls.Add(span);
    listItem.Controls.Add(div);
    listItem.Controls.Add(info);
    unorderedList.Controls.Add(listItem);
}


d.Controls.Add(unorderedList);

在某些时候,我需要检索listItem元素以更改其类属性,我正在尝试这样做:

HtmlGenericControl ul = (HtmlGenericControl)d.FindControl("sortable");
HtmlGenericControl listItem = (HtmlGenericControl)ul.FindControl(ordinalID);
listItem.Attributes["class"] = "item bg-danger";

但是给了我一个关于ul元素的空引用异常......我怎样才能实现我想要的?谢谢!

asp.net nullreferenceexception findcontrol htmlGenericControl

评论

0赞 Josh 10/4/2023
不应该是这样的: HtmlGenericControl ul = (HtmlGenericControl)d.FindControl(“sortable”);
0赞 Gabriele Cozzolino 10/4/2023
@Josh是的,这是一个复制粘贴的错别字,我的错......谢谢你指出来。我编辑了
0赞 Josh 10/4/2023
啊,好的,您是否在调试时检查了元素检查器以检查代码是否调用了正确的名称,并且它们就在那里
0赞 Gabriele Cozzolino 10/4/2023
@Josh我意识到 d 在另一个非 runat div 中时,我按实际情况编辑了标记......我试图将runat添加到另一个div中,但仍然没有运气。元素存在并且它们的 ID 匹配
0赞 Shahram Alemzadeh 10/5/2023
要正确设置 id,请尝试以下操作: 和 .此外,还可以设置如下属性:.unorderedList.ID = "sortable"; listItem.ID = ordinalID[i];listItem.Attributes.Add("class", "item");

答:

0赞 Shahram Alemzadeh 10/6/2023 #1

这可能不是一个真正的答案,但说明了为什么将 ID 设置为属性不起作用,应该设置为属性。

对原始代码进行了一些修改:

<form id="form1" runat="server">
    <div>
        <div class="mt-5">
            <div id="d" runat="server"></div>
        </div>
        <asp: Button ID="btn1" runat="server" Text="Check" OnClick="btn1_Click" />
        <asp: Label id="Lbl1" runat="server"></asp: Label>
    </div>
</form>

创建元素:

string[] parole = { "morning", "good", "StackOverflow!" };
string[] ordinalID = { "2", "1", "3" };
string[] images = { "images/img-1.jpg", "images/img-2.jpg", "images/img-3.jpg" };

void create_elements()
{
    HtmlGenericControl unorderedList = new HtmlGenericControl("ul")
    {
        ID = "sortable",
        ClientIDMode = ClientIDMode.Static
    };
    unorderedList.Attributes.Add("class", "sortable-list");

    Int16 i = -1;
    foreach (string parola in parole)
    {
        ++i;

        HtmlGenericControl listItem = new HtmlGenericControl("li")
        {
            ID = ordinalID[i],
            ClientIDMode = ClientIDMode.Static
        };
        listItem.Attributes.Add("class" , "item");
        listItem.Attributes.Add("draggable" , "true");

        HtmlGenericControl div = new HtmlGenericControl("div");
        div.Attributes.Add("class", "details");
  
        HtmlGenericControl img = new HtmlGenericControl("img");
        img.Attributes.Add("src" , images[i]);

        HtmlGenericControl span = new HtmlGenericControl("span")
        {
            InnerText = parola
        };

        HtmlGenericControl info = new HtmlGenericControl("i");
        info.Attributes.Add("class", "uil uil-draggabledots");

        div.Controls.Add(img);
        div.Controls.Add(span);
        listItem.Controls.Add(div);
        listItem.Controls.Add(info);
        unorderedList.Controls.Add(listItem);
    }
    d.Controls.Add(unorderedList);
}

检查:

protected void btn1_Click(object sender, EventArgs e)
{
    HtmlGenericControl ul = (HtmlGenericControl)d.FindControl("sortable");
    HtmlGenericControl listItem = (HtmlGenericControl)ul.FindControl(ordinalID[0]);
    listItem.Attributes.Add("class", "item bg-danger");
    Lbl1.Text=listItem.Attributes["class"];
}

检查显示 findcontrol 现在可以从 ID 中查找元素:unorderedList

listItem

结果:result