DataList 增删改查、分页

时间:2022-10-20 23:37:46

前端展示如下:

<asp:DataList ID="DataList1" runat="server" BackColor="LightGoldenrodYellow" 
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand"
ondeletecommand="DataList1_DeleteCommand">
<AlternatingItemStyle BackColor="PaleGoldenrod" />
<EditItemTemplate>
<table style="width:100%;">
<tr>
<td style="text-align: right">
编号:</td>
<td>
<asp:TextBox ID="txtID" runat="server" ReadOnly="true" Text='<%# Eval("ID") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
姓名:</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("StuName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
城市:</td>
<td>
<asp:TextBox ID="txtCity" runat="server" Text='<%# Eval("City") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
地址:</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("Address") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
电话号码:</td>
<td>
<asp:TextBox ID="txtPhone" runat="server" Text='<%# Eval("Phone") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<ItemTemplate>
<table style="width:100%;">
<tr>
<td style="text-align: right">
姓名:</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"><%# Eval("StuName") %></asp:LinkButton>
</td>
<td>
<asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit" />
<asp:Button ID="btnDel" runat="server" Text="删除" CommandName="Delete" />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SelectedItemTemplate>
<table style="width:100%;">
<tr>
<td style="text-align: right">
编号:</td>
<td>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right">
姓名:</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("StuName") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right">
城市:</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right">
地址:</td>
<td>
<asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right">
电话号码:</td>
<td>
<asp:Label ID="lblPhone" runat="server" Text='<%# Eval("Phone") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:LinkButton ID="lkbReturn" runat="server" CommandName="Back">返回</asp:LinkButton>
</td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
<br />
共总:<asp:Label ID="lblCount" runat="server"></asp:Label>页——当前:<asp:Label ID="lblNow" runat="server">1</asp:Label>页
<br />
<asp:LinkButton ID="lkbHead" runat="server" Text="首页" onclick="lkbHead_Click"></asp:LinkButton>
<asp:LinkButton ID="lkbPgUp" runat="server" Text="上一页" onclick="lkbPgUp_Click"></asp:LinkButton>
<asp:LinkButton ID="lkbPgDn" runat="server" Text="下一页" onclick="lkbPgDn_Click"></asp:LinkButton>
<asp:LinkButton ID="lkbFoot" runat="server" Text="末页" onclick="lkbFoot_Click"></asp:LinkButton>

后台对应该操作展示如下:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
//把数据全部绑定到DataList上
public void BindData()
{
StudentsBLL sb=new StudentsBLL();
int currentPage =int.Parse(this.lblNow.Text);
//实例化分页数据源
PagedDataSource pds = new PagedDataSource();
//把数据绑定到分页数据源上
pds.DataSource = sb.BindDataList();
//启用分页
pds.AllowPaging = true;
//每一页的记录数
pds.PageSize = 2;
//获取当前页的索引 (这句是关键)
pds.CurrentPageIndex = currentPage - 1;

//四个LinkButton控件启用
this.lkbHead.Enabled = true;
this.lkbPgDn.Enabled = true;
this.lkbPgUp.Enabled = true;
this.lkbFoot.Enabled = true;
//如果第一页,就把“首页”,“上一页”禁用
if(currentPage==1)
{
this.lkbHead.Enabled = false;
this.lkbPgUp.Enabled = false;
}
//如果最后页,就把“末页”,“下一页”禁用
if(currentPage==pds.PageCount)
{
this.lkbFoot.Enabled = false;
this.lkbPgDn.Enabled = false;
}

//把分页数据源绑定到datalist上
this.DataList1.DataSource = pds;
this.DataList1.DataKeyField = "ID";
this.DataList1.DataBind();

this.lblCount.Text = pds.PageCount.ToString();
}
//首页
protected void lkbHead_Click(object sender, EventArgs e)
{
this.lblNow.Text = "1";
BindData();
}
//末页
protected void lkbFoot_Click(object sender, EventArgs e)
{
this.lblNow.Text = this.lblCount.Text;
BindData();
}
//上一页
protected void lkbPgUp_Click(object sender, EventArgs e)
{
this.lblNow.Text =(int.Parse(this.lblNow.Text)-1).ToString();
BindData();
}
//下一页
protected void lkbPgDn_Click(object sender, EventArgs e)
{
this.lblNow.Text = (int.Parse(this.lblNow.Text) + 1).ToString() ;
BindData();
}

//单击服务器控件时激发
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName=="Select")
{
this.DataList1.SelectedIndex = e.Item.ItemIndex;
BindData();
}
if(e.CommandName=="Back")
{
this.DataList1.SelectedIndex = -1;
BindData();
}
}
//编辑
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
//更新
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
StudentsBLL sb = new StudentsBLL();
StudentsModel sm = new StudentsModel();
int id= int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
TextBox tbName =e.Item.FindControl("txtName") as TextBox;
TextBox tbCity = e.Item.FindControl("txtCity") as TextBox;
TextBox tbAddress = e.Item.FindControl("txtAddress") as TextBox;
TextBox tbPhone = e.Item.FindControl("txtPhone") as TextBox;
sm.ID = id;
sm.StuName = tbName.Text;
sm.City = tbCity.Text;
sm.Address = tbAddress.Text;
sm.Phone = tbPhone.Text;
if (sb.StudentsUpdate(sm) > 0)
{
this.DataList1.EditItemIndex =-1;
BindData();
}
else
{
Response.Write("更新失败!");
}
}
//取消
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
BindData();
}
//删除
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
StudentsBLL sb = new StudentsBLL();
int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
if(sb.StudentsDelete(id)>0)
{
BindData();
}
else
{
Response.Write("删除失败!");
}
}
//增加
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("Add.aspx");
}
}