首页 新闻 搜索 专区 学院

在GridView中的每条记录前显示不同的图片

0
悬赏园豆:100 [已解决问题] 解决于 2010-05-14 13:13
代码
前台:
<asp:GridView ID="ResouMenuID" DataKeyNames="ResourceID" runat="server"
AutoGenerateColumns
="false" Width="100%" AllowPaging="True" PageSize="12"
onpageindexchanging
="ResouMenuID_PageIndexChanging"
ondatabound
="ResouMenuID_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" ID="image1" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="ResourceID" DataNavigateUrlFormatString="Resource/Detail.aspx?rid={0}" />
</Columns>
<EmptyDataTemplate>
没有数据!
</EmptyDataTemplate>
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
代码
后台:
DataSet ds;

private void Bind()
{
dbMethod dm
=new dbMethod();
int menuID = int.Parse(Request.QueryString["mID"].ToString());
ds
= dm.getReosurceByMenuIDAndStateID(menuID);
this.ResouMenuID.DataSource = ds;
this.ResouMenuID.DataBind();
}
protected void ResouMenuID_DataBound(object sender, EventArgs e)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Image image
= (Image)this.ResouMenuID.Rows[i].FindControl("image1");

string fileName = ds.Tables[0].Rows[i]["FileName"].ToString();

string ex = fileName.Substring(fileName.IndexOf(".") + 1).ToLower();

if (ex == "doc" || ex == "docx" || ex == "wps")
image.ImageUrl
= "image/resimages/doc.png";
if (ex == "xls" || ex == "xlsx")
image.ImageUrl
= "image/resimages/xls.png";
if (ex == "ppt" || ex == "pptx")
image.ImageUrl
= "image/resimages/ppt/png";
if (ex == "rar" || ex == "zip")
image.ImageUrl
= "image/resimages/rar.jpg";
if (ex == "gif" || ex == "jpg" || ex == "jpeg" || ex == "bmp" || ex == "psd")
image.ImageUrl
= "image/resimages/jpg.png";
if (ex == "rtf" || ex == "txt")
image.ImageUrl
= "image/resimages/txt.png";
if (ex == "pdf")
image.ImageUrl
= "image/resimages/pdf.png";
}
// Bind();
}

小图片存在本地文件夹内,没有存入数据库,在数据库中也没有链接地址。根据文件扩展名决定显示什么图片
问题:不分页一切都没有问题,一旦分页,就出现“索引超出范围”错误,如果加上Bind()方法,就会出现“该页无法显示”的错误。
希望大家帮忙!

scotly的主页 scotly | 初学一级 | 园豆:4
提问于:2010-05-12 16:52
< >
分享
最佳答案
0

后台写方法:

 

protected string GetImgSrc(string fileName){
string ex = fileName.Substring(fileName.IndexOf(".") + 1).ToLower();

if (ex == "doc" || ex == "docx" || ex == "wps")
image.ImageUrl
= "image/resimages/doc.png";
if (ex == "xls" || ex == "xlsx")
image.ImageUrl
= "image/resimages/xls.png";
if (ex == "ppt" || ex == "pptx")
image.ImageUrl
= "image/resimages/ppt/png";
if (ex == "rar" || ex == "zip")
image.ImageUrl
= "image/resimages/rar.jpg";
if (ex == "gif" || ex == "jpg" || ex == "jpeg" || ex == "bmp" || ex == "psd")
image.ImageUrl
= "image/resimages/jpg.png";
if (ex == "rtf" || ex == "txt")
image.ImageUrl
= "image/resimages/txt.png";
if (ex == "pdf")
image.ImageUrl
= "image/resimages/pdf.png";
}

页面中模板里这样绑定:
<img src='<%# GetImgSrc(Eval("FileName").ToString())%>'/>
收获园豆:100
Hawkon | 菜鸟二级 |园豆:225 | 2010-05-13 10:37
虽然有点晚,我已经解决了,但是答案跟我的一样,这样可以实现,分给你了
scotly | 园豆:4 (初学一级) | 2010-05-14 13:13
其他回答(2)
0

相对于页面来说,小图片的路径是否有问题,上面的IF有点雷人。

分页事件里有否有绑定代码?  onpageindexchanging="ResouMenuID_PageIndexChanging"

    protected void ResouMenuID_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ResouMenuID.PageIndex = e.NewPageIndex;
        Bind();//ResouMenuID.DataBind();
    }

 

另外,遍历每行的显示小图标可以在绑定下面写。

测试代码:

<asp:GridView ID="gridTest" runat="server" DataKeyNames="ID" AutoGenerateColumns="false" Width="100%" AllowPaging="True" PageSize="12"
 OnPageIndexChanging="gridTest_PageIndexChanging">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Image runat="server" ID="image1" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerStyle HorizontalAlign="Right" />
</asp:GridView>

CS里写得有点乱,不过是可以的!

protected void Page_Load(object sender, EventArgs e)
{
    GetDt();
    Bind();
}
DataTable dtTemp = new DataTable();
private DataTable GetDt()
{
    DataRow drTemp;
    dtTemp.Columns.Add("ID", typeof(string));
    dtTemp.Columns.Add("FileName", typeof(string));

    for (int i = 0; i < 30; i++)
    {
        drTemp = dtTemp.NewRow();
        drTemp["ID"] = i;
        drTemp["FileName"] = (i % 3 == 0 ? "doc" : "xls");
        dtTemp.Rows.Add(drTemp);
    }
    return dtTemp;
}
private void Bind()
{
    gridTest.DataSource = dtTemp;
    gridTest.DataBind();
    for (int i = 0; i <= gridTest.Rows.Count - 1; i++)
    {
        DataRowView mydrv = dtTemp.DefaultView[i];
        Image image = (Image)gridTest.Rows[i].FindControl("image1");
        string fileName = mydrv["FileName"].ToString();
        if (fileName == "doc")
            image.ImageUrl = "images/page.gif";
        if (fileName == "xls")
            image.ImageUrl = "images/logo.gif";
    }
}
protected void gridTest_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gridTest.PageIndex = e.NewPageIndex;
    Bind();
}

Astar | 园豆:40805 (高人七级) | 2010-05-12 17:05
你改的那个和我那个本质上是一样的,我也试了,不行的
支持(0) 反对(0) scotly | 园豆:4 (初学一级) | 2010-05-12 17:38
@scotly:仔细一看,你ResouMenuID_DataBound事件中的代码就是错的!我修改了下,按照我的写是可以的,刚测试过。
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-05-12 20:25
我改了一下。
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-05-12 20:29
0

在啊GRIDVIEW最前面自定义一模板列 拖一个Image 进去

 

ResouMenuID_DataBound里写

 

  for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

           
string fileName = ds.Tables[0].Rows[i]["FileName"].ToString();

           
string ex = fileName.Substring(fileName.IndexOf(".") + 1).ToLower();
            string Path = "";
           
if (ex == "doc" || ex == "docx" || ex == "wps")
               Path
= "image/resimages/doc.png";
           
if (ex == "xls" || ex == "xlsx")
                Path
= "image/resimages/xls.png";
           
if (ex == "ppt" || ex == "pptx")
                Path
= "image/resimages/ppt/png";
           
if (ex == "rar" || ex == "zip")
               Path
= "image/resimages/rar.jpg";
           
if (ex == "gif" || ex == "jpg" || ex == "jpeg" || ex == "bmp" || ex == "psd")
               Path
= "image/resimages/jpg.png";
           
if (ex == "rtf" || ex == "txt")
               Path
= "image/resimages/txt.png";
           
if (ex == "pdf")
                Path
= "image/resimages/pdf.png";

 

  

 

foreach (Control c in ds.Tables[0].Rows[i].Controls)
        {
                if (c is Image)
                {
                    ((Image)c).ImageUrl= Path;
                }

 

        }

 


}

 

 

驿马 | 园豆:205 (菜鸟二级) | 2010-05-13 12:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册