Question

I am having an issue with the behavior of a GridView between post backs.

The real problem comes from a TemplateField I define in the markup at column[0] with a child CheckBox control. Things work fine for the first, and second search executions. However, at some point between the second execution and anything that causes a post back there after, I lose the contents of the TemplateField.

Its only the the contents of the column and not the whole column itself that gets removed. The TemplateField is present in the source and shows a formated column at position 0 of the table.

CODE:

protected void ExecuteSearch(object sender, EventArgs e)
{
    if (lb_SelectedFields.Items.Count == 0) { return; } //if no selected fields

    //Generates custom SQL query based on user inputs and column Selections
    BuildQuery(); // sets txbSqlText.Text = to the SQL string

    DataTable Table = SqlAdapter.Select(new System.Data.SqlClient.SqlCommand(txbSqlText.Text));

    for (int i = gv_SearchResults.Columns.Count - 1; i > 0; i--) 
    { gv_SearchResults.Columns.RemoveAt(i); } //removes all the columns except[0]

    foreach (ListItem Item in lb_SelectedFields.Items) //adds all the user defined columns
    {
        //Column object that is able to find the column definition
        Column Col = ColumnsBasedOnFocus.FindColumName(Item.Value); 

        if (Col.Type == "HyperLink") { gv_SearchResults.Columns.Add(CreateHyperLinkField(Col)); }
        else { gv_SearchResults.Columns.Add(CreateBoundColumn(Col, true)); } //true is if the column is visable
    }

    gv_SearchResults.DataSource = Table;
    gv_SearchResults.DataBind();
}

ASP.NET:

<asp:GridView ID="gv_SearchResults" runat="server" GridLines="None" CellSpacing="0"
    CellPadding="0" AutoGenerateColumns="false" CssClass="TABLE_LIGHTBLUE" Width="100%">
    <HeaderStyle CssClass="TABLE_LIGHTBLUE_HEADERROW" />
    <Columns>
        <asp:TemplateField ItemStyle-Width="30" ItemStyle-Wrap="false">
            <HeaderTemplate>
                <center>
                    <asp:Button ID="btn_SelectAll" runat="server" OnClick="SelectAll" Text="All" CssClass="TEXT_SMALL" />
                    <asp:CheckBox ID="chk_Placeholder" runat="server" Visible="false" /></center>
            </HeaderTemplate>
            <ItemTemplate>
                <center>
                    <asp:CheckBox ID="chk_Select" runat="server" Visible="true" />
                    <asp:Label ID="lbl_AssetGID" runat="server" Visible="false" Text='<%# Bind("i_GID") %>' /></center>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top