【问题标题】:Hide <TD> when there is nothing in gridview in ASP.NET using VB.NET使用 VB.NET 在 ASP.NET 中的 gridview 中没有任何内容时隐藏 <TD>
【发布时间】:2013-01-08 19:17:30
【问题描述】:

当 gridview 为空时,我试图在表中隐藏一个名为 ImportnatInfo 的 TD。这个gridview 有一个来自数据库表的列来显示。当这个 gridview 为空时,我想隐藏 TD。

下面是asp代码:

<td runat ="server" ID="ImportnatInfo" 
                style="width: inherit; border: 5px double #585858; padding-left: 5px; padding-right: 5px;
                height: inherit; background: #FFFFFF; background-position: center; border-radius: 25px;" 
                enableviewstate="True" visible="False">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None"
                    DataSourceID="ImportantSqlDataSource">
                    <Columns>
                        <asp:BoundField DataField="Importatnat_Info" SortExpression="Importatnat_Info">
                            <ControlStyle BorderStyle="None" Height="10px" />
                            <FooterStyle BorderStyle="None" Height="10px" />
                            <HeaderStyle BorderStyle="None" Height="10px" />
                            <ItemStyle BorderStyle="None" Height="10px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        </asp:BoundField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="ImportantSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AgainConnectionString %>"
                    SelectCommand="SELECT [Importatnat_Info] FROM [StationInfoTable] WHERE ([StationNo] = @StationNo)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlStationNames" Name="StationNo" PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <br />
            </td>

VB.net 代码如下:

If GridView1.Rows.Count = 0 Then
        ImportnatInfo.Visible = False
    Else
        ImportnatInfo.Visible = True
    End If

我有下拉列表,我每次都选择一些东西,所以如果网格中没有可查看的内容,我希望整个 TD 不可见。我试图在它为空时隐藏的 TD,TD 的东西总是可见的,因为当 gridView Vb 中没有任何内容时,添加所以它不会为空。所以我想我应该使用数据源,但仍然不知道该怎么做。知道怎么做吗?

【问题讨论】:

  • 看不到,是问题吗?
  • 我有下拉列表,我每次都选择一些东西,所以如果网格中没有任何东西可以查看,我希望整个 TD 不可见。
  • 是的,但实际发生了什么?
  • 我试图在它为空时隐藏的 TD,它不是并且它总是可见的,因为当那里没有任何东西时 Vb 添加  所以它不会是空的。所以我想我应该是数据源,但仍然无法正常工作。知道谁来做吗?
  • 将单元格设置为可见 false 应该可以解决问题(我认为)。只是为了查看问题是否与 TD 相关,您可以在 TD 中放置一个 asp:Panel 并显示/隐藏它,看看结果是否相同。另外,您调试并看到 GridView1.Rows.Count 绝对为零?

标签: asp.net database data-binding gridview


【解决方案1】:

检查行的类型(通过row.RowType);它可能不是数据行,而是空行或标题行或其他内容。因此,您需要确保 ItemType 为 Item 或 AlternateItem 的行是您计数的类型。您可以查看有关此属性的更多详细信息here。

编辑:您可以使用 LINQ 过滤适当类型的结果:

Dim cnt = GridView1.Rows.Where( _
  Function(i) i.RowType = DataControlRowType.Item OrElse _
              i.RowType = DataControlRowType.AlternateItem).Count()
If cnt = 0 Then
    ImportnatInfo.Visible = False
Else
    ImportnatInfo.Visible = True
End If

【讨论】:

  • 我必须把 Row.ItemType 放在哪里,我需要在 Row.ItemType 之前写什么吗?例如 GridView1.Row.ItemType?如果是,我试过了,但我不能,你想让我做点别的吗?
  • 我把名字弄错了,它是行类型(包括上面的 MSDN 链接)。出于调试目的,为了验证我说的是否正确,当 rows 设置为 1 时,在调试器中执行:GridView1.Rows(0).RowType,然后返回结果。
  • 我检查了它,你是对的,但我该如何使用它?我尝试了几件事,但仍然没有工作。
  • 酷我把你的代码放在 Page_load 中,我得到了“Where”的错误,它说它不是'System.WebUI.WebControls.GridViewRowCollection'的成员
  • 在我得到的错误部分,Lambda 表达式无法转换为“整数”,因为“整数”不是委托类型。
【解决方案2】:

尝试挂钩数据源的Selected 事件

<asp:SqlDataSource ID="ImportantSqlDataSource" OnSelected="SqlDataSource1_Selected" runat="server" ConnectionString="<%$ ConnectionStrings:AgainConnectionString %>"
  SelectCommand="SELECT [Importatnat_Info] FROM [StationInfoTable] WHERE ([StationNo] = @StationNo)">
  <SelectParameters>
        <asp:ControlParameter ControlID="ddlStationNames" Name="StationNo" PropertyName="SelectedValue" Type="Int32" />
  </SelectParameters>

注意OnSelected="SqlDataSource1_Selected"

然后在你的代码后面

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
    ImportnatInfo.Visible = e.AffectedRows > 0

End Sub

即使没有返回数据,您的网格视图也可能有行:页眉、页脚和/或现在找到数据的消息。

更新

也可以试试下面的

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
    ImportnatInfo.Visible = e.AffectedRows > 0
    Gridview1.Visible = e.AffectedRows > 0

End Sub

这也应该将 gridview 设置为在没有行时不显示,无论td 的可见性如何。我还会尝试将您的数据源移到 td 之外,这应该不会有什么不同,但它可能会。

如果在 gridview 被隐藏但 td 仍然可见的情况下尝试此操作后,可能会在页面生命周期的后期发生影响 td 可见性的事情。

【讨论】:

  • TD (ImportantInfo) 是否始终可见?
  • 我尝试了很多很多东西,但似乎我做不到。所以,希望你能很快再次上线。 :)
  • 在你的代码后面的SqlDataSource1_Selected事件上设置一个断点并手动检查e.AffectedRows的值,让我们知道当没有数据返回时你得到什么值。
  • 谢谢我把答案拿出来,我会发布的。感谢您的帮助。
【解决方案3】:
        GridView1.DataBind()
    ImportnatInfo.Visible = True
    If GridView1.Rows.Count >= 1 Then
        If GridView1.Rows(0).Cells.Count >= 1 Then
            If GridView1.Rows(0).Cells(0).Text.Trim() = "&nbsp;" Then
                ImportnatInfo.Visible = False
            Else
                ImportnatInfo.Visible = True
            End If
        End If
    Else
        ImportnatInfo.Visible = False
    End If

您的运行良好,但您只需要添加 Else 以在您检查它不是后显示 TD

【讨论】:

    【解决方案4】:
      GridView1.DataBind()
        ImportantInfo.Visible = True
        If GridView1.Rows.Count = 1 Then
            If GridView1.Rows(0).Cells.Count = 1 Then
                If GridView1.Rows(0).Cells(0).Text.Trim() = "&nbsp;" Then
                    ImportantInfo.Visible = False
    
                End If
    
            End If
        Else
            ImportantInfo.Visible = False
        End If
    

    当 GridView 中没有任何内容时,ASP.NET 总是留下一个空格。所以,我检查它是否为空,如果不是,我检查它是空间还是 GridView 有一些数据。

    【讨论】:

    • 我仍然认为您最好查询数据而不是绑定网格视图。事实上,您最好在 绑定 gridview 之前询问数据
    • 我该怎么做。至少我试试看?
    • 为什么最好在绑定gridview之前查询数据?
    • 快速查看support.microsoft.com/kb/307860,尤其是访问数据部分。使用允许您访问返回的表和行的数据集对象。然后,您可以检查返回的表和行数,如果没有行,则没有绑定数据网格的点,您可以将 td 设置为不可见。我的回答是检查返回的行的另一种方法。另请注意,使用 SQLDataSource 控件时不需要手动绑定网格。这也可能导致问题。
    • 感谢您的解释。我会检查你现在提供的链接。 :)
    猜你喜欢
    • 2019-01-15
    • 1970-01-01
    • 2011-06-25
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多