首页上一页 1 下一页尾页 8 条记录 1/1页
Datalist页脚模板问题
发表在ASP.NET图书答疑
2009-10-16
是否精华
是
否
版块置顶:
是
否
设置时间:
非永久
永久
起始时间:
结束时间:
是否扣分:
是
否
在Datalist页脚模板里加了一个PlaceHolder控件,想通过一个循环加入一排超链接的数字(1 2 3 4....),可是运行出来Datalist页脚模板里却什么都没有,这是哪里错了啊
protected void Page_Load(object sender, EventArgs e)
{
DataList1.DataSource = pds();
DataList1.DataBind();
}
public PagedDataSource pds()
{
string connstring=ConfigurationManager.ConnectionStrings ["SingerConnectionString"].ConnectionString ;
SqlConnection conn=new SqlConnection (connstring );
SqlDataAdapter dap=new SqlDataAdapter ("select * from mysinger",conn );
DataSet ds=new DataSet ();
dap.Fill (ds,"table1");
PagedDataSource pds = new PagedDataSource();
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["Page "]);
pds.PageSize = 5;
pds.AllowPaging = true;
pds.DataSource = ds.Tables["table1"].DefaultView;
pds.CurrentPageIndex = Convert.ToInt32(Request .QueryString ["page"]);
return pds;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
for (int j = 0; j < n;j++ )
{
HyperLink hy = new HyperLink();
Literal lt = new Literal();
int k = j + 1;
hy.Text = k.ToString();
hy.ID = k.ToString();
hy.NavigateUrl = "?page=" + Convert.ToInt32(i);
lt.Text = "";
lt.ID = k.ToString();
ph.Controls.Add(hy);
ph.Controls.Add(lt);
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
DataList1.DataSource = pds();
DataList1.DataBind();
}
public PagedDataSource pds()
{
string connstring=ConfigurationManager.ConnectionStrings ["SingerConnectionString"].ConnectionString ;
SqlConnection conn=new SqlConnection (connstring );
SqlDataAdapter dap=new SqlDataAdapter ("select * from mysinger",conn );
DataSet ds=new DataSet ();
dap.Fill (ds,"table1");
PagedDataSource pds = new PagedDataSource();
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["Page "]);
pds.PageSize = 5;
pds.AllowPaging = true;
pds.DataSource = ds.Tables["table1"].DefaultView;
pds.CurrentPageIndex = Convert.ToInt32(Request .QueryString ["page"]);
return pds;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
for (int j = 0; j < n;j++ )
{
HyperLink hy = new HyperLink();
Literal lt = new Literal();
int k = j + 1;
hy.Text = k.ToString();
hy.ID = k.ToString();
hy.NavigateUrl = "?page=" + Convert.ToInt32(i);
lt.Text = "";
lt.ID = k.ToString();
ph.Controls.Add(hy);
ph.Controls.Add(lt);
}
}
}
}
精彩评论 8
2009-10-17
板凳
DataList控件的脚模板属性是设置为true吧 就是你得先保证该控件的脚模板在运行结果中是要显示的!
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
...
}
}
代码中,你把PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph"); 去掉 在运行下看下能显示数字1.2.3...不?
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
...
}
}
代码中,你把PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph"); 去掉 在运行下看下能显示数字1.2.3...不?
2009-10-17
4L
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
for (int j = 0; j < n;j++ )
{
HyperLink hy = new HyperLink();
Literal lt = new Literal();
int k = j + 1;
hy.Text = k.ToString();
hy.ID = k.ToString();
hy.NavigateUrl = "?page=" + Convert.ToInt32(i);
lt.Text = "";
lt.ID = k.ToString();
ph.Controls.Add(hy);
ph.Controls.Add(lt);
}
} 不用去PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph"); ,在该事件下你写的这段代码是可以显示出1.2.3...但当你单击其中的超链接如数字2 好像不能链接到第二页 也就是说根本没有实现分页显示数据的功能(即你根本没有绑定上数据)!
建议你换一种方法来实现DataList控件分页显示数据功能 方法很多 建议你买一本我们公司的《ASP.NET程序开发范例宝典》第二版
{
int i = pds().CurrentPageIndex;
int n = pds().PageCount;
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph");
for (int j = 0; j < n;j++ )
{
HyperLink hy = new HyperLink();
Literal lt = new Literal();
int k = j + 1;
hy.Text = k.ToString();
hy.ID = k.ToString();
hy.NavigateUrl = "?page=" + Convert.ToInt32(i);
lt.Text = "";
lt.ID = k.ToString();
ph.Controls.Add(hy);
ph.Controls.Add(lt);
}
} 不用去PlaceHolder ph=(PlaceHolder )e.Item .FindControl ("ph"); ,在该事件下你写的这段代码是可以显示出1.2.3...但当你单击其中的超链接如数字2 好像不能链接到第二页 也就是说根本没有实现分页显示数据的功能(即你根本没有绑定上数据)!
建议你换一种方法来实现DataList控件分页显示数据功能 方法很多 建议你买一本我们公司的《ASP.NET程序开发范例宝典》第二版
2009-10-17
5L
绑定数据是在前台中填写的,没有用sqldatasource,数据源是由上面的后台代码编写的,但为什么我的页脚模板里就什么都显示不出来呢?
<ItemTemplate>
SingerID:
<asp:Label ID="SingerIDLabel" runat="server" Text='<%# Eval("SingerID") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
Sex:
<asp:Label ID="SexLabel" runat="server" Text='<%# Eval("Sex") %>' />
<br />
Stature:
<asp:Label ID="StatureLabel" runat="server" Text='<%# Eval("Stature") %>' />
<br />
Photo:
<asp:Label ID="PhotoLabel" runat="server" Text='<%# Eval("Photo") %>' />
<br />
<br />
</ItemTemplate>
<ItemTemplate>
SingerID:
<asp:Label ID="SingerIDLabel" runat="server" Text='<%# Eval("SingerID") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
Sex:
<asp:Label ID="SexLabel" runat="server" Text='<%# Eval("Sex") %>' />
<br />
Stature:
<asp:Label ID="StatureLabel" runat="server" Text='<%# Eval("Stature") %>' />
<br />
Photo:
<asp:Label ID="PhotoLabel" runat="server" Text='<%# Eval("Photo") %>' />
<br />
<br />
</ItemTemplate>
2009-10-17
6L
你这个不是我们图书中源代码问题!我也不知道你具体编写的代码情况!
你换一种方法做下,试试下面的:
前台:
<asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound">
<FooterTemplate>
<table class="style1">
<tr>
<td>
当前页:<asp:Label ID="labCurrentPage" runat="server" Text="Label"></asp:Label>
</td>
<td align="right">
总页:<asp:Label ID="labPageCount" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lkbFirst" runat="server" CommandName="first">第一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbPre" runat="server" CommandName="pre">上一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbNext" runat="server" CommandName="next">下一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbLast" runat="server" CommandName="last">最后一页</asp:LinkButton>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Height="18px" Width="65px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btGO" runat="server" CommandName="gotopage" Text="GO"
/>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<table class="style1">
<tr>
<td class="style2">
用户编号:</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
用户姓名:</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
用户密码:</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("UserPwd") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
你换一种方法做下,试试下面的:
前台:
<asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound">
<FooterTemplate>
<table class="style1">
<tr>
<td>
当前页:<asp:Label ID="labCurrentPage" runat="server" Text="Label"></asp:Label>
</td>
<td align="right">
总页:<asp:Label ID="labPageCount" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lkbFirst" runat="server" CommandName="first">第一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbPre" runat="server" CommandName="pre">上一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbNext" runat="server" CommandName="next">下一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lkbLast" runat="server" CommandName="last">最后一页</asp:LinkButton>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Height="18px" Width="65px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btGO" runat="server" CommandName="gotopage" Text="GO"
/>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<table class="style1">
<tr>
<td class="style2">
用户编号:</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
用户姓名:</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
用户密码:</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("UserPwd") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
2009-10-17
7L
后台:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//引入命名空间
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public SqlConnection con = new SqlConnection("server=.;database=db_BindData;uid=sa;pwd=;");
public PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
BindDataList(0);//首先取页面索引值0,.NET中索引值都是从0开始,它与主键不同
}
protected void BindDataList(int currentpage)
{
pds.AllowPaging = true;//允许分页
pds.PageSize = 3;//每页显示3条数据
pds.CurrentPageIndex = currentpage;//为当前页传入一个int类型的值
//创建数据适配器
SqlDataAdapter dap = new SqlDataAdapter("select * from Users", con);
DataSet ds = new DataSet();
dap.Fill(ds, "Users");//将查询到的数据填充到数据集中
//将数据集中的数据放入分页数据源pds中
pds.DataSource = ds.Tables["Users"].DefaultView;
DataList1.DataSource = pds;//绑定DataList
DataList1.DataKeyField = "id";
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
//以下对用户单击 上一页 下一页等发生的事件进行处理
switch (e.CommandName)
{
case "first"://第一页
pds.CurrentPageIndex=0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre"://上一页
pds.CurrentPageIndex -= 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next"://下一页
pds.CurrentPageIndex += 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last"://最后一页
pds.CurrentPageIndex = pds.PageCount - 1;
break;
case "gotopage"://跳转指定页面
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox txtPage = e.Item.FindControl("TextBox1") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
MyPageNum = int.Parse(txtPage.Text.ToString());
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
BindDataList(pds.CurrentPageIndex - 1);
}
break;
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
Label PageCount = e.Item.FindControl("labPageCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lkbFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lkbPre") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lkbNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lkbLast") as LinkButton;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = pds.PageCount.ToString();//显示总页数
if (pds.IsFirstPage)//如果是第一页 那上一页首页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (pds.IsLastPage)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//引入命名空间
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public SqlConnection con = new SqlConnection("server=.;database=db_BindData;uid=sa;pwd=;");
public PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
BindDataList(0);//首先取页面索引值0,.NET中索引值都是从0开始,它与主键不同
}
protected void BindDataList(int currentpage)
{
pds.AllowPaging = true;//允许分页
pds.PageSize = 3;//每页显示3条数据
pds.CurrentPageIndex = currentpage;//为当前页传入一个int类型的值
//创建数据适配器
SqlDataAdapter dap = new SqlDataAdapter("select * from Users", con);
DataSet ds = new DataSet();
dap.Fill(ds, "Users");//将查询到的数据填充到数据集中
//将数据集中的数据放入分页数据源pds中
pds.DataSource = ds.Tables["Users"].DefaultView;
DataList1.DataSource = pds;//绑定DataList
DataList1.DataKeyField = "id";
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
//以下对用户单击 上一页 下一页等发生的事件进行处理
switch (e.CommandName)
{
case "first"://第一页
pds.CurrentPageIndex=0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre"://上一页
pds.CurrentPageIndex -= 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next"://下一页
pds.CurrentPageIndex += 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last"://最后一页
pds.CurrentPageIndex = pds.PageCount - 1;
break;
case "gotopage"://跳转指定页面
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox txtPage = e.Item.FindControl("TextBox1") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
MyPageNum = int.Parse(txtPage.Text.ToString());
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
BindDataList(pds.CurrentPageIndex - 1);
}
break;
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
Label PageCount = e.Item.FindControl("labPageCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lkbFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lkbPre") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lkbNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lkbLast") as LinkButton;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = pds.PageCount.ToString();//显示总页数
if (pds.IsFirstPage)//如果是第一页 那上一页首页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (pds.IsLastPage)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
}
2009-10-19
8L
更正下给出的示例的后台代码:
原示例代码中的public PagedDataSource pds = new PagedDataSource();
改成:[strong]protect static PagedDataSource pds = new PagedDataSource();
[/strong]
原示例代码中的:
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
[strong]BindDataList(pds.CurrentPageIndex - 1);
[/strong]
更正成:
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
[strong]BindDataList(MyPageNum - 1);[/strong]
原示例代码中在实现页面的跳转时不好使!这样更改后应该没有问题了!
原示例代码中的public PagedDataSource pds = new PagedDataSource();
改成:[strong]protect static PagedDataSource pds = new PagedDataSource();
[/strong]
原示例代码中的:
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
[strong]BindDataList(pds.CurrentPageIndex - 1);
[/strong]
更正成:
if (MyPageNum <= 0 || MyPageNum > pds.PageCount)
Response.Write("<script>alert('请输入数字!')</script>");
else
[strong]BindDataList(MyPageNum - 1);[/strong]
原示例代码中在实现页面的跳转时不好使!这样更改后应该没有问题了!