我想在页面加载时隐藏一个Detailsview控件,其中控件值为0. Detailsview链接到sql数据库,显示的值是从零开始的数值。
我按如下方式设置了控件:
<asp:DetailsView Height="25px" Width="60px" ID="dv2cG" runat="server" AutoGenerateRows="False" DataSourceID="ds2cG" BorderWidth="0px" HorizontalAlign="Center" BackColor="#ccd0ff" >
<Fields>
<asp:BoundField showheader="false" DataField="2cG" HeaderText="2cG" ReadOnly="True" SortExpression="2cG" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="ds2cG" runat="server" ConnectionString="<%$ ConnectionStrings:MaltingsConnectionString %>" SelectCommand="select sum(case when (name = 'English' and ks2en = '2c' and result like 'G') or (name = 'Mathematics' and ks2ma = '2c' and result like 'G') or (not name = 'Mathematics' and not name = 'English' and ks2av = '2c' and result like 'G') then 1 else 0 end) as '2cG' from student join subject on student.upn=subject.upn where @TeachingGroup = 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) OR @TeachingGroup <> 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND ([TeachingGroup] = @TeachingGroup)">
<SelectParameters>
<asp:ControlParameter ControlID="DdlTeachingGroup" Name="TeachingGroup" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DdlYear" Name="StuYear" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ddlDataCollection" Name="DataCollection" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DdlSubject" Name="SubjectName" PropertyName="SelectedValue" />
</SelectParameters>
我写了以下简单程序:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'G column - sets dataitem to visible false where the dataitem equal 0
If dv2cG.DataItem = 0 Then
dv2cG.Visible = False
Else
dv2cG.Visible = True
End If
End Sub
这会成功隐藏DetailsView。但是,即使控件中的值不等于1,它也会这样做。
感谢阅读。
答案 0 :(得分:0)
我在遇到这篇文章here时遇到了这个问题。
我在sql命令上使用了nullif()
函数,例如
select nullif(sum(case when.....then 1 else 0 end),0) as '2cG'