隐藏基于其他列值的显示图像按钮

时间:2014-02-04 03:42:41

标签: asp.net datagridview

我有Grid View我有2列(评分和星级)。在星列中,我显示了5 imagebuttons。我想要的是如果评级是1我想只有一个星(图像按钮)是可见的,如果评级是2然后是2个图像按钮,同样到5。 以下是我的图片TemplateField

<asp:TemplateField>
    <ItemTemplate>                    
        <asp:ImageButton ID="ImageButton" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" />                                       
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" />
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" />
        <asp:ImageButton ID="ImageButton3" runat="server"  ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" />
        <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" />
    </ItemTemplate>
</asp:TemplateField>

1 个答案:

答案 0 :(得分:1)

假设Rating是一个整数,您可以使用内联代码

<asp:TemplateField>
    <ItemTemplate>                    
        <asp:ImageButton ID="ImageButton" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" Visible='<%# int.Parse(Eval("rating").ToString()) >= 1 ? true : false %>' />                                       
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" Visible='<%# int.Parse(Eval("rating").ToString()) >= 2 ? true : false %>' />
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" Visible='<%# int.Parse(Eval("rating").ToString()) >= 3 ? true : false %>' />
        <asp:ImageButton ID="ImageButton3" runat="server"  ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" Visible='<%# int.Parse(Eval("rating").ToString()) >= 4 ? true : false %>' />
        <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/eur/Home/OfferLetter/images/delete_icon.gif" CausesValidation="false" Visible='<%# int.Parse(Eval("rating").ToString()) >= 5 ? true : false %>' />
    </ItemTemplate>
</asp:TemplateField>