2

当 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 中没有任何内容时,它不会为空。所以我想我应该使用数据源,但仍然不知道该怎么做。知道怎么做吗?

4

4 回答 4

2

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

编辑:您可以使用 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
于 2013-01-08T20:36:19.980 回答
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 可见性的事情。

于 2013-01-09T03:44:12.193 回答
1
  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 有一些数据。

于 2013-01-14T15:09:42.283 回答
1
        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  

于 2013-01-15T14:32:42.870 回答