Rectangle 27 5

I was just working through this problem, and none of these solutions would work for me. I couldn't use the EmptyDataTemplate property because I was creating my GridView dynamically with custom fields which provide filters in the headers. I couldn't use the example almny posted because I'm using ObjectDataSources instead of DataSet or DataTable. However, I found this answer posted on another StackOverflow question, which links to this elegant solution that I was able to make work for my particular situation. It involves overriding the CreateChildControls method of the GridView to create the same header row that would have been created had there been real data. I thought it worth posting here, where it's likely to be found by other people in a similar fix.

asp.net - GridView - Show headers on empty data source - Stack Overflo...

asp.net gridview header
Rectangle 27 1

Dynamically created Controls must be recreated on every Page Load, and that includes a PostBack. So if you have the Binding of the GridView inside a IsPostBack (as you would normally), remove it.

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = source;
    GridView1.DataBind();
}

Second you don't need a Literal in the GridView to get the data in the RowDataBound event

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView row = e.Row.DataItem as DataRowView;
    TableCell cantidadesCell = e.Row.Cells[0];

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string[] cantidades = row["CANTIDADES"].ToString().Split(',');

        for (int i = 0; i < cantidades.Length; i++)
        {
            var cantidadTextBox = new TextBox();
            cantidadTextBox.ID = "txtCantidad" + i;
            cantidadTextBox.Text = cantidades[i];
            cantidadTextBox.Width = 50;
            cantidadTextBox.CssClass = "txtCantidad";
            cantidadesCell.Controls.Add(cantidadTextBox);
        }
    }
}

c# - ASP.NET WebForms: Adding controls dynamically to a TableCell of a...

c# asp.net gridview webforms
Rectangle 27 0

Your post is a little vague. It would help to see how you're adding controls to the table. Is it an ASP:Table or a regular HTML table (presumably with a runat="server" attribute since you've successfully added items to it)?

If you intend to let the user make a bunch of selections, then hit a "Submit" button, whereupon you'll process each row based on which row is checked, then you should not be handling the CheckChanged event. Otherwise, as you've noticed, you'll be causing a postback each time and it won't process any of the other checkboxes. So when you create the CheckBox do not set the eventhandler so it doesn't cause a postback.

In your submit button's eventhandler you would loop through each table row, cell, then determine whether the cell's children control contained a checkbox.

I would suggest not using a table. From what you're describing perhaps a GridView or DataList is a better option.

EDIT: here's a simple example to demonstrate. You should be able to get this working in a new project to test out.

<form id="form1" runat="server">
    <div>
    <table id="tbl" runat="server"></table>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
      onclick="btnSubmit_Click" />
    </div>
    </form>
protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        var row = new HtmlTableRow();
        var cell = new HtmlTableCell();
        cell.InnerText = "Row: " + i.ToString();
        row.Cells.Add(cell);
        cell = new HtmlTableCell();
        CheckBox chk = new CheckBox() { ID = "chk" + i.ToString() };
        cell.Controls.Add(chk);
        row.Cells.Add(cell);
        tbl.Rows.Add(row);
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (HtmlTableRow row in tbl.Rows)
    {
        foreach (HtmlTableCell cell in row.Cells)
        {
            foreach (Control c in cell.Controls)
            {
                if (c is CheckBox)
                {
                    // do your processing here
                    CheckBox chk = c as CheckBox;
                    if (chk.Checked)
                    {
                        Response.Write(chk.ID + " was checked <br />");
                    }
                }
            }
        }
    }
}

It is an HTML table. I only want to process checked rows but CheckChanged doesn't work for that either. after adding an asp:Button with a Click handler, I found that it also runs to late (after Form_Load) Maybe I'm going stuff in the wrong place.

CheckChanged won't be triggered since you've set AutoPostBack to false. It must be set to true. If you're okay with posting back on each check change, then you could do that. Otherwise you would process them all at once upon a submit button click. In that case you would process them in a loop similar to what I posted.

I've added a sample to demo how this would work given your HTML table scenario.

CheckChanged
AutoPostBack=false

When AutoPostBack is true the checkbox will post back when you change its state. CheckChanged will be called if the checked state change between postbacks. It doesn't matter if the postback was generated by the AutoPostBack setting or a button click or anything else that generates a postback.

forms - How to programmatically create and use a list of checkboxes fr...

asp.net forms checkbox