How to display image and data in Repeater Control in asp.net & c sharp?

Example of Repeater Control in asp.net.

I have simple solution is bellow.

Displaying image in repeater control try following:


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:HiddenField Value='<%# Eval("id") %>' ID="HiddenField1" runat="server" />
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:Repeater>

And add bellow code into aspx.cs page.


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HiddenField h_field =  e.Item.FindControl("HiddenField1") as HiddenField;
if (h_field != null)
{
string val_item = h_field.Value;
Image img = e.Item.FindControl("Image1") as Image;
img.ImageUrl = "~/image" + val_item + ".jpg";
}

}

About the Author

avatar