Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i have a gridview
i display images in imagebutton
my one line coding is


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

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

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

Note: Make sure Default Image URL is correct.
 
Share this answer
 
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) ?

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