【问题标题】:Why do the gridview still appears even if the datasource is empty?为什么即使数据源为空,gridview 仍然出现?
【发布时间】:2019-11-27 10:18:28
【问题描述】:
                                            <asp:GridView ID="gv_Info" runat="server" Width="1240px" BorderWidth="1px"
                                            CellPadding="4" BorderColor="Gray" AlternatingItemStyle-BackColor="#f6f6eb"
                                            BackColor="#F3372A" AutoGenerateColumns="False" ShowFooter="false">
                                            <AlternatingRowStyle BackColor="White" Wrap="false" />
                                            <Columns>
                                                <asp:TemplateField HeaderText="Select">
                                                   <ItemTemplate>
                                                       <asp:CheckBox ID="chkSelectAsset" runat="server" />
                                                   </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="d" HeaderText="ATag">
                                                    <HeaderStyle HorizontalAlign="Center" />
                                                    <ItemStyle HorizontalAlign="Center" />
                                                </asp:BoundField>
                                                 <asp:BoundField DataField="c" HeaderText="Description">
                                                    <HeaderStyle />
                                                    <ItemStyle HorizontalAlign="Center" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="a" HeaderText="AType">
                                                    <HeaderStyle />
                                                    <ItemStyle HorizontalAlign="Center" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="q" HeaderText="ID">
                                                    <HeaderStyle />
                                                    <ItemStyle HorizontalAlign="Center" />
                                                </asp:BoundField>
                                </Columns>
                            </asp:GridView>


                    gv_InfoAssetsByParams.DataSource = ArrayAssets.ToList()
                    gv_InfoAssetsByParams.DataBind()

但是当 ArrayAssets 为空时,我想让 gridview 为空。所以我这样做,

            If ArrayAssets.Length > 0 Then
                gv_Info.DataSource = ArrayAssets.ToList()
                gv_Info.DataBind()


            Else
              gv_Info.DataSource = ArrayAssets.ToList()
                gv_Info.DataBind()
            End If

但即使我将Nothing 分配给数据源,它仍然会出现。如何使用标题使其为空?

【问题讨论】:

  • else中使用gv_Info.DataSource = null;
  • 为什么If和Else中的代码一样?

标签: asp.net .net vb.net


【解决方案1】:

当为空时,ASP 有一个名为 ShowHeader 的属性。 (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx)
设置ShowHeaderWhenEmpty="false",应该可以了。

【讨论】:

  • @scaryghost 如果答案不起作用,你为什么除了答案?
【解决方案2】:

查看您的代码后,您在 IF 语句中重复了同样的事情。

我试图复制您的问题,实际上,如果数据源为空,则网格不可见。我认为您应该查看 gridview 的属性并从那里开始。同样,我建议在显示数据时使用中继器来更好地控制可见性和布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2018-09-26
    • 2019-12-17
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多