Categories
Asp, Asp.net

Bind Datalist In Asp.Net C# Conditionally

Bind Datalist In Asp.Net C# Conditionally

Hi guys, in this tutorial i will explain you how to Bind Datalist In Asp.Net C# Conditionally.This happens to me when recentlly i am working on a project, and i have to show a image on datalist based on the condition.Let me explain details to you:We have to bind the image in datalist item template image control, but only when the picture has been approved by admin.Else we have to display a different image.

My scenario:

1. Have two column in table, one is picture(where we are storing image name, image has been saved on web folder.)

2. Second is picture_approved.

3. Have to display picture when picture_approved=y, else show a different image.

Solution:

<asp:DataList runat="server" ID ="sngle_profile" RepeatColumns="0">
<ItemTemplate>
<table >
<tr>
<td>
<asp:Image  ID="productimage" runat="server" ImageUrl='<%# (Eval("profile_pic_approved").ToString() == "y") ? "~/Profilepictures/"+Eval("profile") : "~/Profilepictures/noimage.jpg" %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

 

Here noimage.jpg is image that will be displayed when profile is not approved.

One reply on “Bind Datalist In Asp.Net C# Conditionally”

I have bind images to datalist using asp.net in my application which was easy for me to store.I think this is best way also which you provide here and because of this fresher can get more idea.

Comments are closed.