[英]How to add control to the beginning of the collection?
For example; 例如;
panel1.Controls.Add(myControl);
add to end of the collection. 添加到集合的末尾。
Is there a way to add to the beginning of the collection without replacing the one at the beginning? 有没有一种方法可以添加到集合的开头而不替换开头的内容?
panel1.Controls.AddAt(0, myControl)
replaces the control at 0. panel1.Controls.AddAt(0, myControl)
替换为0的控件。
update 更新
actually it seems to work and not replace it. 实际上,它似乎可以正常工作,而不是取代它。 i might have gotten it wrong. 我可能弄错了。
You can use ControlCollection.SetChildIndex
method. 您可以使用ControlCollection.SetChildIndex
方法。
Sets the index of the specified child control in the collection to the specified index value. 将集合中指定的子控件的索引设置为指定的索引值。
When
SetChildIndex
is called, theControl
referred to by the child parameter is moved to the position specified by newIndex and the otherControl
references in theControl.ControlCollection
are reordered to accommodate the move. 调用SetChildIndex
,子参数引用的Control
将移动到newIndex指定的位置,并且Control.ControlCollection
中的其他Control
引用将重新排序以适应该移动。
try this : 尝试这个 :
List<Literal> persistControls = new List<Literal>();
protected void Page_Load(object sender, EventArgs e)
{
display();
}
protected void commentButton_Click(object sender, EventArgs e)
{
Literal myComment = new Literal();
myComment.Text = "<p>" + commentBox.Text + "</p><br />";
commentPanel.Controls.Add(myComment);
persistControls.Insert(0,myComment);
Session["persistControls"] = persistControls;
display();
}
void display()
{
// if you already have some literal populated
if (Session["persistControls"] != null)
{
// pull them out of the session
persistControls = (List<Literal>)Session["persistControls"];
foreach (Literal ltrls in persistControls)
commentPanel.Controls.Add(ltrls); // and push them back into the page
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.