2013-04-15 2 views
3

예 :컬렉션 시작 부분에 컨트롤을 추가하는 방법은 무엇입니까?

panel1.Controls.Add(myControl); 컬렉션의 끝에 추가하십시오.

처음에 컬렉션을 바꾸지 않고 컬렉션의 시작 부분에 추가 할 수있는 방법이 있습니까?

panel1.Controls.AddAt(0, myControl) 0

갱신

에서 제어 실제로 일을하고 그것을 대체 할 것을 대체합니다. 나는 그것을 잘못 이해했을지도 모른다.

답변

-4

시도 : panel1.Controls.insert(0, myControl)

+7

이 ControlCollection''에 대한'insert' 방법이 없습니다. –

3

당신은 ControlCollection.SetChildIndex 방법을 사용할 수 있습니다.

지정된 인덱스 값으로 컬렉션의 지정된 자식 컨트롤 인덱스를 설정합니다. 로 재 배열된다


SetChildIndex가 호출

아이 파라미터에 의해 참조 Control은 newIndex가에 의해 지정된 위치 및 Control.ControlCollection의 다른 Control 참조로 이동되고, 이동을 수용.

+0

이것은 사실이지만 SetChildIndex는 부모 컨트롤을 다시 레이아웃하여 비주얼 셔플을 유발합니다. 부모 컨트롤에 대한 컨트롤 :: SuspendLayout 호출이이를 방지하지 않습니다. SetChildIndex를 먼저 추가하고 이동해야하는 경우 목록 시작 부분에 삽입하는 것이 좋습니다. 두 작업 모두 레이아웃을 발생시킵니다. – uglycoyote

0

이 시도 :

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 
    } 
} 
관련 문제