Jarich
Jarich

Reputation: 337

How to set the visibility of a control in ASP.NET inside the DataList control?

I've got two asp:ImageButton. I want to hide asp:ImageButton ID="ReceiveButton" on each table cell, only if

"<%#Eval("StatusID")=="123" %> " something like this

I do not know how to write this conditional statement in .ASPX file. My code is something like this.

<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>

I tried something like,

<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>

But it doesn't work. How to set properly the condition? Please help me.

Upvotes: 0

Views: 979

Answers (2)

Khaksar Weqar
Khaksar Weqar

Reputation: 425

Try this:

Just Add Visible='<%# Eval("StatusID").ToString().Trim()=="123" %>' to your control ReceiveButton Property.

<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>

Upvotes: 0

Pawan
Pawan

Reputation: 1744

You can use Item ItemDataBound event of 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;
        }
    }
}

And Take a HiddenField on .aspx page as:

<asp:HiddenField ID="hfStatusID" runat="server" Value='<%#Eval("StatusID")%>'/>

Upvotes: 1

Related Questions