0

我想在控件值为 0 的页面加载时隐藏 Detailsview 控件。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 &lt;&gt; '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,它也会这样做。

谢谢阅读。

4

1 回答 1

0

我在这里偶然发现这篇文章时解决了这个问题。

nullif()在 sql 命令上使用了该函数,例如

select nullif(sum(case when.....then 1 else 0 end),0) as '2cG'
于 2013-04-24T14:12:40.147 回答