Click here to Skip to main content
14,241,806 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi
i have a gridview
i display images in imagebutton
my one line coding is


<asp:ImageButton ID="Image1" Height="50" Width="50" CommandName="ViewDetails" CommandArgument='<%#Eval("Reg_Id") %>'

                                ImageUrl='<%#Eval("profileimage") %>' AlternateText="No Image" runat="server" />



my questin is that
if image is not in database table then how can i display default image

means
that

if in database table "profileimage" collumn is null then i want to display a default image(images/face.png")
thanks
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Do like below...
ImageUrl='<%#AssignImage(Eval("profileimage").ToString()) %>'

And in code Behind...
public string AssignImage(string profileimage)
{
    return !string.IsNullOrEmpty(profileimage) ? profileimage : "images/face.png";
}

Note: Make sure Default Image URL is correct.
   
v2
Comments
Manish Dalwadi 11-Apr-14 11:59am
   
m sorry
i never done this type of coding...so please help me for solve this
its showing error..
.
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0122: 'frm_admin_Edit_User_Details.AssignImage(string)' is inaccessible due to its protection level
   
Oops!!! Update the function access specifier to public. I am updating the answer now.
Manish Dalwadi 11-Apr-14 12:00pm
   
<telerik:gridtemplatecolumn xmlns:telerik="#unknown">
<itemtemplate>
<asp:ImageButton ID="Image1" Height="50" Width="50" CommandName="ViewDetails" CommandArgument='<%#Eval("Reg_Id") %>'
ImageUrl='<%# AssignImage(Eval("profileimage").ToString()) %>' AlternateText="No Image"
runat="server" />

Manish Dalwadi 11-Apr-14 12:00pm
   
private string AssignImage(string profileimage)
{
return !string.IsNullOrEmpty(profileimage) ? profileimage : "images/default_profile.png";
}
Rahul VB 12-Apr-14 2:45am
   
Hey Dash,
I just have one doubt: can you explain me the significance of :
!string.IsNullOrEmpty(profileimage) ?
   
It just checks if the profileimage value is null or empty.
Rahul VB 12-Apr-14 4:28am
   
Ohhh very nice, thanks a ton.
-Rahul


   
Most welcome buddy. :)

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100