简体   繁体   English

asp.net-GridView动态页脚行创建问题

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

I am able to create BoundFields and Footer-rows dynamically like this in my GridView: 我可以在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);
            }
        }
....................................
....................................
....................................
}

But the problem is, when I click the "Add New" - button, it disappears instantly. 但是问题是,当我单击“添加新的”按钮时,它会立即消失。 And, also I am unable to add any event handler to it. 而且,我也无法向其添加任何事件处理程序。 Or intercept its actions like this: 或拦截其行为如下:

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();
            }
        }

One more thing, I have seen some solutions that suggests to handle the GridView's rowBound event. 还有一件事,我看到了一些建议处理GridView的rowBound事件的解决方案。 But I need to do it from within Page_load event handler, or in, GridView1_RowCommand event handler. 但是我需要从Page_load事件处理程序中或GridView1_RowCommand事件处理程序中执行此操作。

Move your code from Page_Load to Page_Init . 将代码从Page_Load移到Page_Init Things added in the Page_Load last only for the lifecycle of one postback. Page_Load添加的内容仅在一次回发的生命周期内可持续。

You'd then be able to add eventhandlers, intercept events etc. 然后,您将能够添加事件处理程序,拦截事件等。

Dynamically created controls mus be re-created on every postback . 动态创建的控件可以在每次回发时重新创建 Your "Add New" button causes a postback so the dynamically created footer disappears. 您的“添加新”按钮会导致回发,因此动态创建的页脚会消失。 Is there a reason this grid has to be created dynamically? 是否有理由必须动态创建此网格? From the code you posted it appears that you could do this in markup instead. 从发布的代码看来,您可以改为在标记中执行此操作。 If not, you'll have to re-create the dynamic controls on every postback. 如果不是,则必须在每次回发中重新创建动态控件。

Edited to add: I played with this a little bit and what's below works in that the grid doesn't disappear and events are handled, but it doesn't actually do anything. 编辑添加:我玩了一点,下面的工作原理是,网格不会消失并且事件不会被处理,但实际上并没有做任何事情。 Hope this helps. 希望这可以帮助。

Markup: 标记:

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

Code: 码:

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