2017-04-24 105 views
0

我有一個頁面,我需要創建一個視圖,我們可以添加行並且該視圖需要像這樣。asp.net gridview中的兩行itemtemplate和footertemplate

enter image description here

下面第二行有一個很大的文本。在填寫數據並單擊添加新鏈接時,該行應保存在數據庫中並且可見,並且應該出現新的頁腳行以填充新數據。 我通過谷歌和SO,發現ListView,DataList,但我無法找到一種方法來實現這一點。我知道這個問題可能是重複的。但我想通過屏幕截圖顯示我需要的內容。

請多幫助我一點,指導正確的方向。謝謝。

回答

1

您可以在GridView的RowCreated事件中執行此操作。但額外的行將放置在正常頁眉和頁腳行的上方。在這裏,如果需要,還可以將控件添加到額外的行。但請記住,必須在每個PostBack上重新創建動態創建的控件,因此數據綁定不得在IsPostBack檢查中進行。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    //cast the sender back to a gridview 
    GridView gv = sender as GridView; 

    //check if the row is the header row 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     //create a new row 
     GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); 
     extraHeader.BackColor = Color.Red; 

     //loop all the columns and create a new cell for each 
     for (int i = 0; i < gv.Columns.Count; i++) 
     { 
      TableCell cell = new TableCell(); 
      cell.Text = "ExtraHeader " + i; 

      //add the cell to the new header row 
      extraHeader.Cells.Add(cell); 
     } 

     //add the new row to the gridview 
     gv.Controls[0].Controls.AddAt(0, extraHeader); 
    } 

    //check if the row is the footer row 
    if (e.Row.RowType == DataControlRowType.Footer) 
    { 
     //create a new row 
     GridViewRow extraFooter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert); 
     extraFooter.BackColor = Color.Green; 

     //add one cell with colspan 2 
     TableCell cell1 = new TableCell(); 
     cell1.Text = "ExtraFooter 1"; 
     cell1.ColumnSpan = 2; 
     extraFooter.Cells.Add(cell1); 

     //add another one with colspanning the rest 
     TableCell cell2 = new TableCell(); 
     cell2.Text = "ExtraFooter 2"; 
     cell2.ColumnSpan = gv.Columns.Count - 2; 
     extraFooter.Cells.Add(cell2); 

     //add +2 to the row count. one for the extra header and 1 for the extra footer 
     int insertIndex = gv.Rows.Count + 2; 

     //add the new row to the gridview 
     gv.Controls[0].Controls.AddAt(insertIndex, extraFooter); 
    } 
} 
相關問題