提问人:Gabriele Cozzolino 提问时间:10/4/2023 最后编辑:Gabriele Cozzolino 更新时间:10/6/2023 访问量:38
asp.net runat div 中找到控制权
asp.net find control inside runat div
问:
我的标记中有这个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元素的空引用异常......我怎样才能实现我想要的?谢谢!
答:
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"];
}
评论
unorderedList.ID = "sortable";
listItem.ID = ordinalID[i];
listItem.Attributes.Add("class", "item");