Asp.net 在gridview中显示默认img
我将在gridview中列出我的所有用户,我将为那些没有个人资料图片的用户显示一个默认图像 我已经看过这个解决方案,但我无法让它工作,我做错了什么 我的代码Gridview控件 代码隐藏OnRowDataBound:Asp.net 在gridview中显示默认img,asp.net,gridview,Asp.net,Gridview,我将在gridview中列出我的所有用户,我将为那些没有个人资料图片的用户显示一个默认图像 我已经看过这个解决方案,但我无法让它工作,我做错了什么 我的代码Gridview控件 代码隐藏OnRowDataBound: 您可以在图像控件本身中使用三值运算符来检查是否有图像,如果没有,则显示默认图像 <asp:Image ID="img" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("fldImg").ToString
您可以在图像控件本身中使用三值运算符来检查是否有图像,如果没有,则显示默认图像
<asp:Image ID="img" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("fldImg").ToString()) ? "../img/profil/" + Eval("fldImg") : "../img/profil/default-billede/ingen-profil-billede.png" %>' />
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (GridViewRow gvr in gwListeOverBruger.Rows)
{
Image image = (Image)gvr.FindControl("img");
if (image != null && image.ImageUrl == "")
{
image.ImageUrl = "../img/profil/default-billede/ingen-profil-billede.png";
}
}
}
}
protected void gwListeOverBruger_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image profilImg = (Image)e.Row.FindControl("img");
if (profilImg != null && profilImg.ImageUrl == "")
{
profilImg.ImageUrl = "../img/profil/default-billede/ingen-profil-billede.png";
}
}
}
<asp:Image ID="img" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("fldImg").ToString()) ? "../img/profil/" + Eval("fldImg") : "../img/profil/default-billede/ingen-profil-billede.png" %>' />