I am trying to hide a TD named ImportnatInfo in a table when the gridview is empty. This gridview has one column from a table in a database to show. When this gridview is empty I want to hide the TD.
当gridview为空时,我试图在表中隐藏名为ImportnatInfo的TD。此gridview有一列来自数据库中的表以显示。当这个gridview为空时我想隐藏TD。
The following is the asp code:
以下是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>
And the VB.net code is the following:
VB.net代码如下:
If GridView1.Rows.Count = 0 Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
I have dropdownlist and i choose something everytime so if there is nothing in the grid to view, i want the whole TD to be invisible. the TD that im trying to hide when it is empty, the thing the TD is always visible because when there is nothing in the gridView Vb add so it won't be empty. so i thought i should use the datasource and still not sure what to do. Any idea how to do that?
我有下拉列表,我每次都选择一些东西,所以如果网格中没有任何内容可供查看,我希望整个TD都是隐形的。我试图隐藏的TD是空的,TD总是可见的,因为当gridView Vb中没有任何内容添加时它不会为空。所以我认为我应该使用数据源,但仍然不知道该怎么做。知道怎么做吗?
4 个解决方案
#1
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() = " " Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
End If
Else
ImportnatInfo.Visible = False
End If
Yours is working good but you just need to add Else to show the TD after you check it is not
你的工作正常,但你需要添加Else才能在检查后显示TD
#2
2
Check the type of row (via row.RowType
); it's probably not a data row, but an empty row or header row or something else. So you need to make sure rows with an ItemType of Item or AlternateItem are the type you are counting. You can see more detailed information about this property here.
检查行的类型(通过row.RowType);它可能不是数据行,而是空行或标题行或其他内容。因此,您需要确保ItemType为Item或AlternateItem的行是您计算的类型。您可以在此处查看有关此属性的更多详细信息。
EDIT: You can use LINQ to filter the results of the appropriate type:
编辑:您可以使用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
#3
2
Try hooking into the Selected event of your data source
尝试连接到数据源的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>
Note OnSelected="SqlDataSource1_Selected"
注意OnSelected =“SqlDataSource1_Selected”
Then in your code behind
然后在你的代码后面
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
Your gridview may have rows even if no data is returned: headers, footers and/or a now data found message.
即使没有返回数据,您的gridview也可能有行:页眉,页脚和/或现在找到数据的消息。
UPDATE
UPDATE
Also try the following
还请尝试以下方法
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
This should also set the gridview to not show when there are no rows, regardless of the visibility of the td
. I would also try moving your datasource outside of the td
it shouldn't make a difference, but it may.
这也应该将gridview设置为在没有行时不显示,无论td的可见性如何。我也会尝试将数据源移到td之外它不应该有所作为,但它可能会。
If after trying this if the gridview is hidden, but the td remains visible there may be something happening later in the page life cycle that is affecting the visibility of the td.
如果在尝试此操作后如果网格视图被隐藏,但td仍然可见,则页面生命周期中可能会发生一些影响td可见性的事情。
#4
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() = " " Then
ImportantInfo.Visible = False
End If
End If
Else
ImportantInfo.Visible = False
End If
ASP.NET always leave a space when there is nothing in the GridView. So, I check if it is empty or not if not i check if it is the space or the GridView has some data.
当GridView中没有任何内容时,ASP.NET总是留一个空格。所以,我检查它是否为空,如果不是我检查它是否是空格或GridView有一些数据。
#1
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() = " " Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
End If
Else
ImportnatInfo.Visible = False
End If
Yours is working good but you just need to add Else to show the TD after you check it is not
你的工作正常,但你需要添加Else才能在检查后显示TD
#2
2
Check the type of row (via row.RowType
); it's probably not a data row, but an empty row or header row or something else. So you need to make sure rows with an ItemType of Item or AlternateItem are the type you are counting. You can see more detailed information about this property here.
检查行的类型(通过row.RowType);它可能不是数据行,而是空行或标题行或其他内容。因此,您需要确保ItemType为Item或AlternateItem的行是您计算的类型。您可以在此处查看有关此属性的更多详细信息。
EDIT: You can use LINQ to filter the results of the appropriate type:
编辑:您可以使用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
#3
2
Try hooking into the Selected event of your data source
尝试连接到数据源的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>
Note OnSelected="SqlDataSource1_Selected"
注意OnSelected =“SqlDataSource1_Selected”
Then in your code behind
然后在你的代码后面
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
Your gridview may have rows even if no data is returned: headers, footers and/or a now data found message.
即使没有返回数据,您的gridview也可能有行:页眉,页脚和/或现在找到数据的消息。
UPDATE
UPDATE
Also try the following
还请尝试以下方法
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
This should also set the gridview to not show when there are no rows, regardless of the visibility of the td
. I would also try moving your datasource outside of the td
it shouldn't make a difference, but it may.
这也应该将gridview设置为在没有行时不显示,无论td的可见性如何。我也会尝试将数据源移到td之外它不应该有所作为,但它可能会。
If after trying this if the gridview is hidden, but the td remains visible there may be something happening later in the page life cycle that is affecting the visibility of the td.
如果在尝试此操作后如果网格视图被隐藏,但td仍然可见,则页面生命周期中可能会发生一些影响td可见性的事情。
#4
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() = " " Then
ImportantInfo.Visible = False
End If
End If
Else
ImportantInfo.Visible = False
End If
ASP.NET always leave a space when there is nothing in the GridView. So, I check if it is empty or not if not i check if it is the space or the GridView has some data.
当GridView中没有任何内容时,ASP.NET总是留一个空格。所以,我检查它是否为空,如果不是我检查它是否是空格或GridView有一些数据。