如何在DataList控件中设置ASP.NET中控件的可见性?

时间:2014-10-01 06:24:12

标签: c# asp.net c#-4.0

我有两个asp:ImageButton。我想在每个表格单元格上隐藏asp:ImageButton ID="ReceiveButton",仅在

时隐藏

"<%#Eval("StatusID")=="123" %> "类似这样的事情

我不知道如何在.ASPX文件中编写这个条件语句。我的代码是这样的。

<td>
      <%#Eval("StatusID")%>
</td>
<td align="center">

     <asp:ImageButton ID="ReceiveButton" ToolTip="Receive/process this aproved PO" runat="server"     
     ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/receive.png" 
     CommandName="CommandReceived" />

    <asp:ImageButton ID="DetailButton" ToolTip="View Approved PO" runat="server" 
    ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/view.png"  CommandName="PODetails" />

</td>

我尝试了类似的东西,

<td>
      <%#Eval("StatusID")%>
</td>
<td align="center">

    <%if (Eval("StatusID") == "123") { %>
     <asp:ImageButton ID="ReceiveButton" ToolTip="Receive/process this aproved PO" runat="server"     
     ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/receive.png" 
     CommandName="CommandReceived" />
    <%} %>

    <asp:ImageButton ID="DetailButton" ToolTip="View Approved PO" runat="server" 
    ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/view.png"  CommandName="PODetails" />

</td>

但它不起作用。如何正确设置条件?请帮我。

2 个答案:

答案 0 :(得分:1)

您可以使用[{1}}

的项ItemDataBound事件
DataList

并在protected void DatalistID_ItemDataBound(object sender, DataListItemEventArgs e) { HiddenField hfStatusID= e.Item.FindControl("hfStatusID") as HiddenField; ImageButton ReceiveButton= e.Item.FindControl("ReceiveButton") as ImageButton; if (hfStatusID!= null && ReceiveButton!=null) { if (hfStatusID.Value == "123") // As per your Requirement { ReceiveButton.Visible= false; } } } 页上点击HiddenField

.aspx

答案 1 :(得分:0)

试试这个:

只需将Visible='<%# Eval("StatusID").ToString().Trim()=="123" %>'添加到您的控件ReceiveButton媒体资源中。

<td>
  <%#Eval("StatusID")%>
</td>
<td align="center">

 <asp:ImageButton ID="ReceiveButton" ToolTip="Receive/process this aproved PO" runat="server"     
 ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/receive.png" 
 CommandName="CommandReceived" Visible='<%# Eval("StatusID").ToString().Trim()=="123" %>' />

<asp:ImageButton ID="DetailButton" ToolTip="View Approved PO" runat="server" 
ImageUrl="~/DesktopModules/HBI_PurchaseOrder/Assets/Images/view.png"  CommandName="PODetails" />

</td>