views:

158

answers:

2

I am able to create BoundFields and Footer-rows dynamically like this in my GridView:

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. But I need to do it from within Page_load event handler, or in, GridView1_RowCommand event handler.

+1  A: 

Move your code from Page_Load to Page_Init. Things added in the Page_Load last only for the lifecycle of one postback.

You'd then be able to add eventhandlers, intercept events etc.

Jan Jongboom
+1  A: 

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();
}
Jamie Ide
That article relates to .Net 1.1, in .Net 2 and up, use object creation on `Page_Init` for this kind of constructions.
Jan Jongboom
I don't think dynamic control usage changed and Page_Init was always the recommended event in which to create dynamic controls. The key point is that they have to be re-created on each PostBack; Init is recommended because ViewState is restored after this event. I may be wrong, I've always found a way to get by without dynamically creating controls because they're so difficult to work with.
Jamie Ide