Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to check if session["username"] = something.
if so image link will be 'A' else will be 'B'.

here is my code:

XML
<asp:DataList ID="DataList1"
runat="server" ShowHeader="False" CellSpacing="2">
<HeaderTemplate>
Images ...
</HeaderTemplate>
<ItemStyle />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl='<%# Eval("image_path","~/Images/{0}")%>' style="padding: 2px;" Width="50px" Height="50px" />
</ItemTemplate>
</asp:DataList>


my c# code:

C#
con.Open();
cmd = new SqlCommand("select Image_path from table WHERE image_id = something ", con);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();

// How do i write this ?

if (session["username"] == something)
{
  // Datalist.ImageButton1.ImageUrl = Eval("image_path","/location A")
}
else
{
  // Datalist.ImageButton1.ImageUrl = Eval("image_path","/location B")
}
Posted

Try something like this ;)
C#
foreach (DataListItem item in myDataList.Items)
{
    ImageButton myImageButton = (ImageButton )item.FindControl("ImageButton1");
    if (session["username"] = something)
    {
        myImageButton.ImageUrl = ...
    }
    else
    {
        myImageButton.ImageUrl = ...
    }
}
 
Share this answer
 
Comments
Member 10210318 21-Feb-14 7:12am    
Thanks a lot, let me check it up.
Member 10210318 21-Feb-14 7:43am    
Ok, it is working fine, thanks.

is there a way to set the myImageButton.ImageUrl to show photos from two different locations ?
for example myImageButton.ImageUrl = ~/Images/{0}" and ~/Images_old/{0}" ?
Ahmed Bensaid 21-Feb-14 8:27am    
You're welcome !
Leave your ImageUrl property empty in your markup.
And fill it only in your code behind ;)
Member 10210318 21-Feb-14 8:37am    
ok, but how do i give two locations in the code ?
Ahmed Bensaid 21-Feb-14 9:03am    
foreach (DataListItem item in myDataList.Items)
{
ImageButton myImageButton = (ImageButton )item.FindControl("ImageButton1");
if (session["username"] = something)
{
myImageButton.ImageUrl = "~/Images/your_image"
}
else
{
myImageButton.ImageUrl = "~/Images_old/your_image"
}
}
Hi try this

XML
<asp:DataList ID="dlList" runat="server" ShowHeader="False" CellSpacing="2" OnItemDataBound="dlListItemBinding">
        <ItemTemplate>
            <asp:ImageButton ID="ImageButton1" runat="server" style="padding: 2px;" Width="50px" Height="50px" />
        </ItemTemplate>
    </asp:DataList>



C#
protected void dlListItemBinding(object sender, DataListItemEventArgs e)
 {
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
     if (Session["value"] == "A")
     {
 ((ImageButton)e.Item.FindControl("ImageButton1")).ImageUrl = "~/Image/ImageA.png"; // change the path here
     }
     else
     {
 ((ImageButton)e.Item.FindControl("ImageButton1")).ImageUrl = "~/Image/ImageB.png"; // change the path here
     }
  }
 }
 
Share this answer
 
v2
Comments
Member 10210318 21-Feb-14 8:05am    
Ok, it is working fine, thanks. is there a way to set the myImageButton.ImageUrl to show photos from two different locations ? for example myImageButton.ImageUrl = ~/Images/{0}" and ~/Images_old/{0}" ?
Manish Kumar Namdev 24-Feb-14 1:49am    
Hi, as I could understood your query, you just need to set the path and to set the two image from two different location you would have a condition that probably you would be checking.

Because one at a time you can show only a single image.

As you said "For example myImageButton.ImageUrl = ~/Images/{0}" and ~/Images_old/{0}" ?" this can be done in the above method.

And please elaborate more and tell the clear situation what exactly you are trying to do and want to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900