[英]Hide <TD> when there is nothing in gridview in ASP.NET using VB.NET
當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中沒有添加內容時,它就不會為空。 所以我認為我應該使用數據源,但仍然不確定該怎么做。 任何想法如何做到這一點?
檢查行的類型(通過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
嘗試掛接到數據源的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
即使沒有返回任何數據,您的gridview也可能有行:頁眉,頁腳和/或現在發現數據的消息。
UPDATE
也嘗試以下
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的可見性。
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
當GridView中沒有任何內容時,ASP.NET總是會留一個空間。 因此,我檢查它是否為空,如果不是,請檢查它是否為空格或GridView是否包含一些數據。
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
您的設備運行良好,但是您只需在未檢查到TD后再添加Else即可顯示它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.