GridView 在回发期间丢失列内容

发布于 2025-01-07 22:55:33 字数 2557 浏览 1 评论 0 原文

我对回发之间的 GridView 行为有疑问。

真正的问题来自于我在 column[0] 的标记中定义的 TemplateField 以及子 CheckBox 控件。第一次和第二次搜索执行时一切正常。然而,在第二次执行和之后导致回发的任何事情之间的某个时刻,我丢失了 TemplateField 的内容。

它只是列的内容,而不是整个列本身被删除。 TemplateField 存在于源中,并在表的位置 0 处显示格式化列。

代码:

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2025-01-14 22:55:33

找到一个描述类似情况的链接。

https://connect.microsoft.com/VisualStudio/feedback/details/104994/templatefield-in-a-gridview-doesnt-have-its-viewstate-restored-when-boundfields-are-inserted#详细信息

它们描述了 ASP.Net 代码中的一个错误,该错误无法使用动态生成的网格视图中的模板字段正确管理视图状态。

Found a link that describes a similar situation.

https://connect.microsoft.com/VisualStudio/feedback/details/104994/templatefield-in-a-gridview-doesnt-have-its-viewstate-restored-when-boundfields-are-inserted#details

They describe a bug in ASP.Net code that fails to properly manage view states with template fields in dynamically generated grid views.

烟若柳尘 2025-01-14 22:55:33

基本上,TemplateFields 无法从 ViewState 正确恢复,并且如果您以编程方式修改 ASPX 声明的列,它也无法从声明创建它们。
我可以使用的唯一解决方案是创建一个从 TemplateField 派生的新类,该类在构造函数中将 ItemTemplate 设置为 ITemplate 派生类,这意味着必须以编程方式而不是声明方式定义模板。
您还可以在每次回发时重新绑定 gridview,但这是它自己的蠕虫。

Basically TemplateFields can't be properly restored from ViewState, and if you modify the ASPX-declared columns programmatically, it can't create them from the declarations either.
The only solution I could get working was to create a new class deriving from TemplateField that in the constructor set the ItemTemplate to an ITemplate-derived class, which means having to define the template programmatically instead of declaratively.
You can also rebind the gridview on each postback but that's its own can of worms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文