Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在gridview中显示默认图像_Asp.net_Image_Gridview - Fatal编程技术网

Asp.net 在gridview中显示默认图像

Asp.net 在gridview中显示默认图像,asp.net,image,gridview,Asp.net,Image,Gridview,如果用户未提供任何图像,如何在gridview中显示已解除的配置文件图像 if (fileUpload.PostedFile == null) { lblStatus.Text = "No file specified."; return; } else { { 一种方法可能是在RowDataBound事件期间检查每一行,以查看图像是否存在。如果为空,则可以指定默认图像url protected void GridView1_RowDataBound(

如果用户未提供任何图像,如何在gridview中显示已解除的配置文件图像

if (fileUpload.PostedFile == null) 
{ 
    lblStatus.Text = "No file specified."; 
    return; 
} 
else
{        
{

一种方法可能是在RowDataBound事件期间检查每一行,以查看图像是否存在。如果为空,则可以指定默认图像url

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{        
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        Image image= (Image)e.Row.FindControl("ImageForPerson");

        if (image != null && image.ImageIrl == "")
        {
            image.ImageUrl = // default image url goes here
        }
    }
}
不要忘记将RowDataBound事件添加到GridView定义中

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" />

这也可以在aspx本身的一行中完成。通过使用三元运算符

<asp:Image ID="Image1" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("userImage").ToString()) ? "/images/" + Eval("userImage") : "/images/noimage.jpg" %>' />


请发布您现在使用的代码示例。em使用asp上载控件并将图像插入数据库,如果用户未提供图像,则会将空值插入数据库表。。相反,我想在用户未提供的情况下显示一个已解除的图像。好的,我认为我的解决方案应该适合您。绑定GridView时,图像url将为空,因为用户未提供任何url。但是,正如我所说的,您可以在RowDataBound事件中检查它,如果它为null,您可以给它一个默认url来显示。“if(fileUpload.PostedFile==null){lblStatus.Text=“没有指定文件。”return;}else{”如何在此场景中调用该事件谢谢,代码段工作正常只是想知道为什么在使用Page_Load event时出现此错误。foreach语句无法对“System.Web.UI.WebControl.GridView”类型的变量进行操作,因为“System.Web.UI.WebControl.GridView”不包含“GetEnumerator”的公共定义欢迎使用。我忘记了put.Rows,这就是为什么会出现异常。请参阅我更新的foreach语句。
<asp:Image ID="Image1" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("userImage").ToString()) ? "/images/" + Eval("userImage") : "/images/noimage.jpg" %>' />