簡體   English   中英

asp.net-GridView動態頁腳行創建問題

[英]asp.net - GridView dynamic footer row creation problem

我可以在GridView中動態創建BoundField和Footer行:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreateGridView();
            }
        }

        private void CreateGridView()
        {
            GridView1.Columns.Clear();

            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            CommandField cf = new CommandField();
            cf.ShowEditButton = true;

            GridView1.Columns.Add(cf);

            int colCount = 1;
            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;
                //boundField.FooterText = "---";

                if (colCount == 3 || colCount == 5)
                {
                    boundField.ReadOnly = true;
                }

                GridView1.Columns.Add(boundField);
                colCount++;
            }

            GridView1.ShowFooter = true;

            GridView1.DataSource = dataTable;
            GridView1.DataBind();

            GridViewRow footerRow = GridView1.FooterRow;
            Button b = new Button();
            b.Text = "Add New";
            int i = 0;
            footerRow.Cells[i].Controls.Add(b);
            foreach (DataColumn c in dataTable.Columns)
            {
                ++i;
                TextBox tb = new TextBox();
                footerRow.Cells[i].Controls.Add(tb);
            }
        }
....................................
....................................
....................................
}

但是問題是,當我單擊“添加新的”按鈕時,它會立即消失。 而且,我也無法向其添加任何事件處理程序。 或攔截其行為如下:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "Edit")
            {
                GridView1.EditIndex = index;

                GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

                //We can get cell data like this
                string id = selectedRow.Cells[1].Text;
                string isbn = selectedRow.Cells[2].Text;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
            else if (e.CommandName == "Update")
            {
                LinkButton updateButton = (LinkButton)e.CommandSource;

                DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;

                GridViewRow gvr = (GridViewRow)dcfc.Parent;

                //The update...................
                //Update grid-data to database
                UpdateDataInTheDatabase(gvr.Cells[1].Controls);                

                //Grid goes back to normal
                GridView1.EditIndex = -1;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
        }

還有一件事,我看到了一些建議處理GridView的rowBound事件的解決方案。 但是我需要從Page_load事件處理程序中或GridView1_RowCommand事件處理程序中執行此操作。

將代碼從Page_Load移到Page_Init Page_Load添加的內容僅在一次回發的生命周期內可持續。

然后,您將能夠添加事件處理程序,攔截事件等。

動態創建的控件可以在每次回發時重新創建 您的“添加新”按鈕會導致回發,因此動態創建的頁腳會消失。 是否有理由必須動態創建此網格? 從發布的代碼看來,您可以改為在標記中執行此操作。 如果不是,則必須在每次回發中重新創建動態控件。

編輯添加:我玩了一點,下面的工作原理是,網格不會消失並且事件不會被處理,但實際上並沒有做任何事情。 希望這可以幫助。

標記:

    <p><asp:Literal ID="Literal1" runat="server" /></p>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        OnRowCommand="GridView1_RowCommand" 
        OnRowEditing="GridView1_RowEditing"/>

碼:

protected void Page_Load(object sender, EventArgs e)
{
    BindGridView();
}

private DataTable GetBooksDataTable()
{
    var dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Title", typeof(string));
    dt.Columns.Add("Author", typeof(string));

    for (int index = 0; index < 10; index++)
    {
        dt.Rows.Add(index, "Title" + index, "Author" + index);
    }
    return dt;
}

private void BindGridView()
{
    var dt = GetBooksDataTable();

    GridView1.Columns.Clear();
    GridView1.ShowFooter = true;

    var cf = new CommandField();
    cf.HeaderText = "Action";
    cf.ShowEditButton = true;
    GridView1.Columns.Add(cf);

    for (int index = 0; index < dt.Columns.Count; index++)
    {
        var boundField = new BoundField();
        boundField.DataField = dt.Columns[index].ColumnName;
        boundField.HeaderText = dt.Columns[index].ColumnName;
        GridView1.Columns.Add(boundField);
    }

    GridView1.DataSource = dt;
    GridView1.DataBind();

    var footer = GridView1.FooterRow;
    var b = new LinkButton();
    b.Text = "Add New";
    b.CommandName = "Add New";
    footer.Cells[0].Controls.Add(b);
    for (int index = 1; index < dt.Columns.Count + 1; index++)
    {
        var tb = new TextBox();
        footer.Cells[index].Controls.Add(tb);
    }

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Literal1.Text = e.CommandName;
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
}

暫無
暫無

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

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