using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Titlepage_List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillData();
}
}
static PagedDataSource Pds;
void FillData()
{
SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["musicConnectionString"].ToString());
Conn.Open();
SqlDataAdapter Sda = new SqlDataAdapter("GetAllCdFace", Conn);
Sda.SelectCommand.Parameters.Add("@diskType", SqlDbType.Int).Value = lblListMessage.Text.ToString();
Sda.SelectCommand.CommandType = CommandType.StoredProcedure();
DataSet Ds = new DataSet();
Sda.Fill(Ds);
Pds = new PagedDataSource();
Pds.DataSource = Ds.Tables[0].DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = 4;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
Conn.Close();
}
//下一页
protected void LinkButton2_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex++;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}
//上一页
protected void LinkButton1_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex--;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}
}[要求]
我想显示一个4行5列的datalist,分页,里面显示的都是图片
[简单来说就是每页20张图片]
当我点击一下其中一张图片,就跳转到另一个页面,同时把id传过去我的存储过程GetAllCdFace搜索到的记录为一个id和一个url地址
后台基本上是这样[不知道对不对],那么我前台怎么弄啊前台DataList1的RepeatColumns=5 我已经设置好了,接下来不知道怎么做了不知道我说的够不够详细~分数可以再加,无所谓~
private void BangDing()
{
int eid = Convert.ToInt32(Request.QueryString["Eid"]);
DataSet ds = ProductBF.GetProductMsg(eid);
if (ds.Tables[0].Rows.Count > 0)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 8;
int cpage;
if (Request.QueryString["id"] != null)
{
cpage = Convert.ToInt32(Request.QueryString["id"]);
}
else cpage = 1; pds.CurrentPageIndex = cpage - 1; if (!pds.IsFirstPage)
pre.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + Convert.ToInt32(cpage - 1);
if (!pds.IsLastPage)
next.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + Convert.ToInt32(cpage + 1);
if (!pds.IsFirstPage)
first.NavigateUrl = Request.CurrentExecutionFilePath + "?id=1";
if (!pds.IsLastPage)
last.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + Convert.ToInt32(pds.PageCount); this.dltCPZS.DataSource = pds;
this.dltCPZS.DataBind();
}
}<table class="B" align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="height:100px">
<tr>
<td valign="top" style="height: 117px">
<asp:DataList ID="dltCPZS" runat="server" Height="100px" RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="5" width="100%">
<tr>
<td align="center">
<a target="_blank" href="ShowOneChanPin.aspx?cpimg=<%#Eval("Pimage") %>">
<img alt="<%#Eval("Pname")%>" style="border:0" src="../CPphotos/<%#Eval("Pimage") %>" width="120px" height="100px"/>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td colspan="3" align="right">
<asp:HyperLink ID="first" runat="server">第一页</asp:HyperLink>
<asp:HyperLink ID="pre" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">最后一页</asp:HyperLink>
</td>
</tr>
</table>
比如你只是要输出一个分页的图片列表
你的datatable可以就只有一个字段,
这个字段可以是字符串的<a href="picshow.aspx?id=###" target="_blank"><img src="####" /></a>
然后把这个datatable绑定上去就可以了。
比如你只是要输出一个分页的图片列表
你的datatable可以就只有一个字段,
这个字段可以是字符串的<a href="picshow.aspx?id=###" target="_blank"><img src="####" /></a>
然后把这个datatable绑定上去就可以了。————————————————————————————————
广告:本人正在找工作,(asp.net/C#/SQL Server)WEB开发方向。 在CSDN有简历:
http://job.csdn.net/Con001_ProjectManage/job/PreviewResume.aspx?ResumeID=c26d4a0f-6dae-4df4-b9d1-55b34440f917 email:[email protected]
Datalist的<ItemTemplate>里面:
<a href="连接地址?ID=<%#DataBinder.Eval(Container.DataItem,"ID")%><img src=<%#DataBinder.Eval(Container.DataItem,"图片地址字段") %> > </a>
这样行不行?
datalist里面全是图片,点击图片,把这幅图片相对应的id【存储过程里搜索到的字段】传到其他页面【不用考虑,另一个页面只要接收id就好了】
试试webdiyer的
LZ要求是把id传到其它页面
页面传值?解决方法差不多就那么几种了
<asp:Image ID="imgDiskpictureurl" runat="server" ImageUrl='<%# <%#DataBinder.Eval(Container.DataItem,"diskpictureurl") %> %>' ToolTip="" /></a>
运行以后:
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS1040: 预处理器指令必须作为一行的第一个非空白字符出现源错误: 行 60: <ItemTemplate>
行 61: <a href='"titlepage_show.aspx?diskid=" + <%# DataBinder.Eval(Container.DataItem,"diskid")%>'>
行 62: <asp:Image ID="imgDiskpictureurl" runat="server" ImageUrl='<%# <%#DataBinder.Eval(Container.DataItem,"diskpictureurl") %> %>' ToolTip="" /></a>
行 63: </ItemTemplate>
行 64: </asp:DataList>
不知道我的后台需要改什么吗?
这一句好像有点问题吧??
这样应该就可以了吧
我确实是想做分页,并且同时想做成能把id传递的功能!(再说像我这么纯洁的人怎么会骗大家呢,哈哈~~~~)
<asp:Image ID="imgDiskpictureurl" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"diskpictureurl") %>' ToolTip="" /></a>
图片能够出来了,但是一点就:路径中具有非法字符。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentException: 路径中具有非法字符。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="News_test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 752px; POSITION: absolute; TOP: 16px; HEIGHT: 312px" cellSpacing="0" cellPadding="0" width="752" border="0">
<TR>
<TD style="HEIGHT: 29px"><FONT face="宋体">DataList分页技术和超级链接</FONT></TD>
</TR>
<TR>
<TD style="HEIGHT: 252px">
<asp:datalist id="DataList1" runat="server" Width="576px" Height="96px">
<HeaderTemplate>
定单编号<td>
员工编号<td>
定单日期<td>
运费<td>
运往所在城市
</HeaderTemplate> <ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"OrderID")%> <td>
<%# DataBinder.Eval(Container.DataItem,"CustomerID")%> <td>
<%# DataBinder.Eval(Container.DataItem,"OrderDate")%> <td>
<%# DataBinder.Eval(Container.DataItem,"Freight")%> <td>
<%# DataBinder.Eval(Container.DataItem,"ShipCity")%>
</ItemTemplate>
</asp:datalist>
</TD>
</TR>
<TR>
<TD><FONT face="宋体"> <asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton>
<asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton>
<asp:linkbutton id="NextLB" runat="server" OnCommand=LinkButton_Click CommandName="next">下一页</asp:linkbutton>
<asp:linkbutton id="EndLB" runat="server" OnCommand=LinkButton_Click CommandName="end">最后一页</asp:linkbutton>
总<asp:label id="TotalLbl" runat="server"></asp:label>页 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>页
<asp:linkbutton id="JumpLB" runat="server" OnCommand=LinkButton_Click CommandName="jump">跳到</asp:linkbutton>第
<asp:textbox id="TextBox1" runat="server" Width="90px"></asp:textbox>
页</FONT></TD>
</TR>
</TABLE>
</div>
</form>
</body>
</html>
-------------------------------------------------
后台代码:
----------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class News_test : System.Web.UI.Page
{
int CurrentPage;//当前页数
int PageSize; //每页条数
int PageCount; //总页数
int RecordCount;//总条数
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
PageSize = 10;//每页10条记录
if (!Page.IsPostBack)
{
CurrentPage = 0;//当前页习惯设为0
ViewState["PageIndex"] = 0;//页索引也设为0
//计算总共有多少记录
RecordCount = CalculateRecord();
//计算总共有多少页
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
} this.TotalLbl.Text = PageCount.ToString();//显示总页数
ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
}
}
//计算总共有多少条记录
private int CalculateRecord()
{
try
{
int recordCount;
SqlConnection con = new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=sa");//数据库使用Northwind;
con.Open(); string sql = "select count(*) as count from Orders";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read())
{
recordCount = Int32.Parse(sdr["count"].ToString());
}
else
{
recordCount = 0;
} sdr.Close();
con.Close();
return recordCount;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//将数据绑定到Datalist控件
public void DataListBind()
{
try
{
int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
string sql = "select * from Orders";
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=sa");
con.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(ds, StartIndex, PageSize, "orders");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
this.DataList1.DataSource = ds.Tables["orders"].DefaultView;
this.DataList1.DataBind();
this.PreviousLB.Enabled = true;
this.NextLB.Enabled = true;
if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数 }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
PageCount = (int)ViewState["PageCount"];//获得总页数
string cmd = e.CommandName; //判断cmd,以判定翻页方向
switch (cmd)
{
case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
break; case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
break; case "first"://第一页
CurrentPage = 0;
break; case "end"://最后一页
CurrentPage = PageCount - 1;
break; case "jump"://跳转到第几页
if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
{
return;
}
else
{
CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
break;
}
}
ViewState["PageIndex"] = CurrentPage;//获得当前页 this.DataListBind();//重新将DataList绑定到数据库
}}
http://blog.csdn.net/caoxicao/archive/2006/04/14/663035.aspx
改成:<a href='titlepage_show.aspx?diskid= <%# DataBinder.Eval(Container.DataItem,"diskid")%> '>就能运行了