简体   繁体   English

如何将控件添加到集合的开头?

[英]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, the Control referred to by the child parameter is moved to the position specified by newIndex and the other Control references in the Control.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.

相关问题 如何在datagrid控件的每一行的开头添加复选框控件? - How to add checkbox control at the beginning of every row in datagrid control? 如何将集合的设计器属性添加到我的服务器控件中? - How to add a designer property for a collection to my server control? 如何在UltraTreeNode集合中添加自定义属性(Infragistics UltraTree Control) - How to add a custom property in UltraTreeNode Collection (Infragistics UltraTree Control) 如何从集合中随机获取字符串,但更喜欢集合开头的字符串 - How to randomly get a string from a collection but prefer strings at the beginning of the collection 如何将属性自定义控件设置为从头开始 - how to set property custom control to be from beginning 控件属性是Collection。 集合更改时如何添加更改事件? - Control property is Collection. How to add change event when the collection changes? 如何创建具有集合的控件 - How to create a control that has a collection 如何将项添加到ListBox列表的开头? - How to add item to the beginning of the list in ListBox? 如何将选项卡页动态添加到tabcontrol的开头 - How to add tabpages dynamically to the beginning of the tabcontrol 如何将项目添加到列表的开头<T> ? - How to add item to the beginning of List<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM