簡體   English   中英

asp.net gridview中的兩行itemtemplate和footertemplate

[英]Two rows in asp.net gridview itemtemplate and footertemplate

我有一個頁面需要創建一個視圖,可以在其中添加行,並且該視圖必須看起來像這樣。

在此處輸入圖片說明

這里第二行有一個大文本框。 填充數據並單擊“添加新鏈接”后,該行應保存在DB中並可見,並且新的頁腳行應出現以填充新數據。 我通過google和SO,找到了ListView,DataList,但我想不出一種方法來實現這一點。 我知道這個問題可能是重復的。 但是我想通過屏幕截圖顯示我需要的東西。

請為我提供更多幫助,並指導正確的方向。 謝謝。

您可以在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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM