RepeatDirection:项的布局方向。
RepeatColumns:该布局的列的数目,默认为0.即不限制。
使用 DataList 控件显示模板定义的数据绑定列表。DataList 控件支持选择和编辑。 DataList 控件的内容可以通过使用模板操控。下表列出了支持的模板。
ItemTemplate为默认模版,如果绑定了数据,则在该模版中编辑显示项目。
模板名称 | 说明 |
---|---|
AlternatingItemTemplate | 如果已定义,则为 DataList 中的交替项提供内容和布局。如果未定义,则使用 ItemTemplate。 |
EditItemTemplate | 如果已定义,则为 DataList 中当前编辑的项提供内容和布局。如果未定义,则使用 ItemTemplate。 |
FooterTemplate | 如果已定义,则为 DataList 的脚注部分提供内容和布局。如果未定义,将不显示脚注部分。 |
HeaderTemplate | 如果已定义,则为 DataList 的页眉节提供内容和布局。如果未定义,将不显示页眉节。 |
ItemTemplate | 为 DataList 中的项提供内容和布局所要求的模板。 |
SelectedItemTemplate | 如果已定义,则为 DataList 中当前选定项提供内容和布局。如果未定义,则使用 ItemTemplate。 |
SeparatorTemplate | 如果已定义,则为 DataList 中各项之间的分隔符提供内容和布局。如果未定义,将不显示分隔符。 |
- 分页显示DataList控件中的数据
该分页代码页适用于Repeater控件等其他数据类控件。页面源代码添加一个DataList控件(DataList1),在页脚模版中添加翻页控制相关按钮。
<FooterTemplate>
<%--页脚模板--%>
<table border="0" cellpadding="0" cellspacing="0" style="width: 580px">
<tr>
<td colspan="2" style="font-size: 12px;">
<hr />
共
<asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>
页 当前为第
<asp:Label ID="lblp" runat="server" Text="Label"></asp:Label>
页
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hlla" runat="server" Text="末页"></asp:HyperLink>
</td>
</tr>
</table>
</FooterTemplate>
cs代码:
private PagedDataSource pds()
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
PagedDataSource pds = new PagedDataSource();
//初始化一个PagedDataSource,允许控件分页
pds.DataSource = myDs.Tables[0].DefaultView;
//将上面的ds转换成标准数据视图
pds.AllowPaging = true;
//允许分页
pds.PageSize = 5;
//每页大小为5
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//设置当前页
return pds;
//将处理完毕的pds对象发出去
}
protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (Request.QueryString["name"] == null)
{
if (e.Item.ItemType == ListItemType.Footer)
{//判断当前项是页脚模板
int n = pds().PageCount;//将分页总数赋给变量n
int i = pds().CurrentPageIndex;//将当前分页码赋给i
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
//找到lblpc这个Label,将总页码赋给他
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
//这里调整为自己网页需要到的连接
hlfir.NavigateUrl = "?Type=" + Server.UrlEncode(Request.QueryString["Type"]) + "&page=0";
HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
hlla.NavigateUrl = "?Type=" + Server.UrlEncode(Request.QueryString["Type"]) + "&page=" + Convert.ToInt32(n - 1);
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
HyperLink hln = (HyperLink)e.Item.FindControl("hln");
//找到表示上页和下页这两个控件
if (i <= 0)
{//如果当前页已经是第0页
hlp.Enabled = false;
hlfir.Enabled = false;
hln.Enabled = true;
}
else
{
hlp.NavigateUrl = "?Type=" + Server.UrlEncode(Request.QueryString["Type"]) + "&page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
{
hln.NavigateUrl = "?Type=" + Server.UrlEncode(Request.QueryString["Type"]) + "&page=" + Convert.ToInt32(i + 1);
}
}
}
}
显示、编辑、更新数据
在DataList模版中添加LinkButton控件,并为其绑定数据,设置CommandName属性和Click事件来实现对数据的显示、编辑、更新、取消。在DataList中CommandName接受默认值为:Edit、Delete、Update、Cancel。如果值不为上述4种,则在ItemCommand中自行定义。
在页面源代码中添加DataList控件(DataList1),设置了ItemTemplate、EditItemTemplate、SelectedItemTemplate三种模版,分别对应默认显示模版,编辑模版和选择模版。
<asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="AccessDataSource1"
OnItemCommand="DataList1_ItemCommand" OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand" OnEditCommand="DataList1_EditCommand">
<EditItemTemplate>
//用于录入name字段的值;点击Button_up,完成修改;点击Button_ca,退出EditItemTemplate模版,回到ItemTemplate模版。
<asp:TextBox ID="TextBox_name" runat="server"></asp:TextBox>
<asp:Button ID="Button_up" runat="server" CommandName="update" Text="更新" />
<asp:Button ID="Button_ca" runat="server" CommandName="cancel" Text="取消" />
</EditItemTemplate>
<SelectedItemTemplate>
//显示选中行的详细信息
Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
</SelectedItemTemplate>
<ItemTemplate>
//默认显示数据库中id字段值,点击LinkButton_se,显示详细信息SelectedItemTemplate模版,进入;点击Button_ed,进入EditItemTemplate模版。
Id: <asp:LinkButton ID="LinkButton_se" runat="server" CommandName="select" Text='<%# Eval("Id") %>' OnClick="LinkButton2_Click">
</asp:LinkButton><asp:Button ID="Button_ed" runat="server" CommandName="edit" Text="编辑" />
</ItemTemplate>
</asp:DataList>
cs代码:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.SelectedIndex = -1;
DataList1.DataBind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
//取得编辑行的关键字段的值
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
//取得文本框中输入的内容
string name = ((TextBox)e.Item.FindControl("TextBox_name")).Text;
//更新数据库
string sql = "update test set name='" + name + "'";
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
myConn.Open();
string sqlStr = "select * from test";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
//取消编辑状态
DataList1.SelectedIndex = -1;
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.SelectedIndex = e.Item.ItemIndex;
DataList1.DataBind();
}