前台:
<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()方法,就会出现“该页无法显示”的错误。
希望大家帮忙!
后台写方法:
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())%>'/>
相对于页面来说,小图片的路径是否有问题,上面的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();
}
在啊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;
}
}
}