在gridview中显示默认图像 [英] Displaying a default image in gridview

查看:124
本文介绍了在gridview中显示默认图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(fileUpload.PostedFile == null)
{
lblStatus.Text =没有指定文件。;
return;
}
else
{
{


解决方案

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

pre $ protected $ GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

if(e.RowType == DataControlRowType.DataRow)
{
Image image =(Image)e.Row.FindControl(ImageForPerson);

if(image!= null& image.ImageIrl ==)
{
image.ImageUrl = //默认图片网址在这里
}
}
}

不要忘记将RowDataBound事件添加到GridView

 < asp:GridView ID =GridView1OnRowDataBound =GridView1_RowDataBound/> 






或者如果您不想使用RowDataBound事件。在Page_Load中,您可以手动检查GridView的每一行并逐个检查ImageUrl。

  protected void Page_Load(object sender,EventArgs e)
{
foreach(GridViewRow gvr in GridView1.Rows)
{
Image image =(Image)gvr.FindControl(ImageForPerson);

if(image!= null& image.ImageIrl ==)
{
image.ImageUrl = //默认图片网址在这里
}
}
}


How to display a defualt profile image in gridview if user didn't provided any image.

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

解决方案

One approach might be to check each row during RowDataBound event to see image exists or not. If it is empty, you can assign a default image 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
        }
    }
}

Do not forget to add RowDataBound event to your GridView definition.

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


Or If you do not want to use RowDataBound event. At Page_Load you can manually go through each Row of GridView and check ImageUrl one by one.

protected void Page_Load(object sender, EventArgs e)
{
     foreach(GridViewRow gvr in GridView1.Rows)
     {
         Image image = (Image)gvr.FindControl("ImageForPerson");

         if (image != null && image.ImageIrl == "")
         {
             image.ImageUrl = // default image url goes here
         }
     }
}

这篇关于在gridview中显示默认图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆