这周不算太忙,就动手联系了一下Repeter的分页和排序,顺便利用中介实现了增,删,改的功能。分页和排序主要用到了PagedDataSource类和DataView对象,这样实现起来就不是很复杂了。做的比较简陋,有需要的朋友可以看一下。
效果图如下:


前台代码:
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Height="50px" Visible="False" Width="1029px">
<asp:Label ID="lblCompanyName" runat="server" Text="CompanyName" Visible="False"></asp:Label>
<asp:TextBox ID="txtCompanyName" runat="server" Visible="False"></asp:TextBox>
<asp:Label ID="lblName" runat="server" Text="ContactName" Visible="False"></asp:Label>
<asp:TextBox ID="txtContactName" runat="server" Visible="False"></asp:TextBox>
<asp:Label ID="lblAddress" runat="server" Text="Address" Visible="False"></asp:Label>
<asp:TextBox ID="txtAddress" runat="server" Visible="False"></asp:TextBox>
<asp:Label ID="lblCity" runat="server" Text="City" Visible="False"></asp:Label>
<asp:TextBox ID="txtCity" runat="server" Visible="False" Width="129px"></asp:TextBox>
<asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="Update" OnClick="btnUpdate_Click" />
<asp:Button ID="btnInsert" runat="server" Text="新增" OnClick="btnInsert_Click" />
<asp:Button ID="Button1" runat="server" Text="取消" CommandName="Cancel" OnClick="Button1_Click" />
</asp:Panel>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"
OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th align="left">
SupplierID</th>
<th align="left">
CompanyName</th>
<th align="left">
ContactName</th>
<th align="left">
Address</th>
<th align="left">
City</th>
<th>
</th>
<th>
</th>
<th>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="Item" runat="server">
<td>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("SupplierID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblName" runat="server" Text=' <%#Eval("ContactName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Edit" runat="server" Text="编辑" CommandName="Edit" /></td>
<td>
<asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" /></td>
<td>
<asp:Button ID="New" runat="server" Text="新增" CommandName="New" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8" id="Alter" runat="server">
<td>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("SupplierID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblName" runat="server" Text=' <%#Eval("ContactName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Edit" runat="server" Text="编辑" CommandName="Edit" /></td>
<td>
<asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" /></td>
<td>
<asp:Button ID="New" runat="server" Text="新增" CommandName="New" /></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label ID="lblPage" runat="server"></asp:Label>
<asp:Label ID="lblCount" runat="server"></asp:Label>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="上一页" />
<asp:Button ID="btnPage" runat="server" OnClick="btnPage_Click" Text="下一页" />
<asp:Button ID="btnSort" runat="server" Text="排序" OnClick="btnSort_Click" /></div>
</form>
后台代码:
string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection Con;

protected void Page_Load(object sender, EventArgs e)

...{
if (!IsPostBack)

...{
//设置默认的排序顺序
ViewState["CompanyName"] = "SupplierID";
ViewState["Direction"] = "ASC";
ViewState["LineNo"] = 0;
//初始化的时候默认显示第一页
lblCount.Text = "1";
BindRepeter();
}
}

//判断命令
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

...{
String Index = ((Label)Repeater1.Items[e.Item.ItemIndex].FindControl("lblID")).Text;
Session["ID"] = Index;
//编辑模式
if (e.CommandName == "Edit")

...{
string CompanyName = ((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblCompanyName"))).Text;
string Name = ((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblName"))).Text;
string Address = ((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblAddress"))).Text;
string City = ((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblCity"))).Text;
txtCompanyName.Text = CompanyName;
txtContactName.Text = Name;
txtAddress.Text = Address;
txtCity.Text = City;
if (Panel1.Visible == false)

...{
ShowMessage("进入编辑模式");

}
SetDisplayTrue();
btnInsert.Visible = false;
btnUpdate.Visible = true;
Button1.Visible = true;
}
//删除
else if (e.CommandName == "Delete")

...{
if (Panel1.Visible == true)

...{
ShowMessage("编辑模式下禁止删除");
}
else

...{
SetDisplayFalse();
string DeleteStr = "DELETE FROM Suppliers WHERE SupplierID='" + Index + "'";
Con = new SqlConnection(ConStr);
SqlCommand DeleteCmd = new SqlCommand(DeleteStr,Con);
try

...{
Con.Open();
DeleteCmd.ExecuteNonQuery();
ShowMessage("删除成功");
BindRepeter();
}
catch (Exception ex)

...{
ShowMessage("删除有错误,请检查");
}
finally

...{
Con.Dispose();
}
}
}//新增
else if (e.CommandName == "New")

...{
if (Panel1.Visible == true)

...{
ShowMessage("编辑模式下禁止添加新行");
}
else

...{
SetDisplayTrue();
ClearWord();
btnInsert.Visible = true;
btnUpdate.Visible = false;
Button1.Visible = true;
}

}
}

//绑定Repeter
private void BindRepeter()

...{
string QueryCon = "SELECT SupplierID,CompanyName,ContactName,Address,City FROM Suppliers";
Con = new SqlConnection(ConStr);
SqlDataAdapter NorthWindDa = new SqlDataAdapter(QueryCon, ConStr);
DataSet Ds = new DataSet();
NorthWindDa.Fill(Ds, "Suppliers");
//Repeater1.DataKeyNames = new string[] { "SupplierID" };
DataView Dv = Ds.Tables["Suppliers"].DefaultView;
//排序表达式
string SortExpress = (string)ViewState["CompanyName"] + " " + (string)ViewState["Direction"];
Dv.Sort = SortExpress;
//绑定数据源
//Repeater1.DataSource = Ds.Tables["Suppliers"];
//利用PagedDataSource进行分页
PagedDataSource PageDs = new PagedDataSource();
PageDs.DataSource = Dv;
PageDs.AllowPaging = true;
PageDs.PageSize = 10;
int CurrentPage = Convert.ToInt32(lblCount.Text);
//当前页
PageDs.CurrentPageIndex = CurrentPage-1;
btnPage.Enabled = true;
Button2.Enabled = true;
if (CurrentPage == 1)

...{
Button2.Enabled = false;
}
if (CurrentPage == PageDs.PageCount)

...{
btnPage.Enabled = false;
}
//PageCount = PageDs.PageCount;
lblPage.Text = "共" + PageDs.PageCount + "页,当前在第";
//将PageDs作为数据源进行绑定
Repeater1.DataSource = PageDs;
Repeater1.DataBind();
}

//显示编辑区域
private void SetDisplayTrue()

...{
lblCompanyName.Visible = true;
lblName.Visible = true;
lblAddress.Visible = true;
lblCity.Visible = true;
txtCompanyName.Visible = true;
txtContactName.Visible = true;
txtAddress.Visible = true;
txtCity.Visible = true;
Panel1.Visible = true;
}

//隐藏编辑区域
private void SetDisplayFalse()

...{
lblCompanyName.Visible = false;
lblName.Visible = false;
lblAddress.Visible = false;
lblCity.Visible = false;
txtCompanyName.Visible = false;
txtContactName.Visible = false;
txtAddress.Visible = false;
txtCity.Visible = false;
Panel1.Visible = false;
}

private void ClearWord()

...{
txtCompanyName.Text = "";
txtContactName.Text = "";
txtAddress.Text = "";
txtCity.Text = "";
}
//封装javascript
private void ShowMessage(string Message)

...{
Literal TxtMsg = new Literal();
TxtMsg.Text = "<script>alert('"+Message+"')</script>";
Page.Controls.Add(TxtMsg);
}


//更新数据
protected void btnUpdate_Click(object sender, EventArgs e)

...{

/**//*在有数据输入的时候应该检查输入数据的合法性,
* 过滤掉特殊的脚本,特殊的字符串,提高安全性
*/
string CompanyName = Server.HtmlEncode(txtCompanyName.Text.ToString());
string ContactName = Server.HtmlEncode(txtContactName.Text.ToString());
string Address = Server.HtmlEncode(txtAddress.Text.ToString());
string City = Server.HtmlEncode(txtCity.Text.ToString());
string UpdateStr = "UPDATE Suppliers SET CompanyName='" + CompanyName + "',ContactName='" + ContactName + "'," +
"Address='" + Address + "',City='" + City + "' WHERE SupplierID='" + Session["ID"] + "' ";
Con = new SqlConnection(ConStr);
SqlCommand UpdateCmd = new SqlCommand(UpdateStr,Con);
try

...{
Con.Open();
UpdateCmd.ExecuteNonQuery();
ShowMessage("更新成功");
SetDisplayFalse();
BindRepeter();
}
catch (Exception ex)

...{
ShowMessage("更新出错,请重新输入");
}
finally

...{
Con.Dispose();
}
}


protected void Button1_Click(object sender, EventArgs e)

...{
SetDisplayFalse();
}

//插入一行新数据
protected void btnInsert_Click(object sender, EventArgs e)

...{
string CompanyName = Server.HtmlEncode(txtCompanyName.Text.ToString());
string ContactName = Server.HtmlEncode(txtContactName.Text.ToString());
string Address = Server.HtmlEncode(txtAddress.Text.ToString());
string City = Server.HtmlEncode(txtCity.Text.ToString());
string InsertStr = "INSERT INTO Suppliers (CompanyName,ContactName,Address,City) VALUES('" + CompanyName + "','" + ContactName + "','" + Address + "','" + City + "')";
Con = new SqlConnection(ConStr);
SqlCommand InsertCmd = new SqlCommand(InsertStr, Con);
try

...{
Con.Open();
InsertCmd.ExecuteNonQuery();
ShowMessage("插入成功");
SetDisplayFalse();
BindRepeter();
}
catch (Exception ex)

...{
ShowMessage("插入失败,请重新检查");
}
finally

...{
Con.Dispose();
}
}

//给删除按钮添加客户端的Confirm对话框
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)

...{

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

...{
((Button)Repeater1.Controls[Repeater1.Controls.Count-1].FindControl("Delete")).Attributes["onclick"] = "if(!confirm('你真的要删除这条记录么?'))return false;";
}

}


//下一页
protected void btnPage_Click(object sender, EventArgs e)

...{
//更改pageds的当前页数
lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) + 1);
BindRepeter();
}

//上一页
protected void Button2_Click(object sender, EventArgs e)

...{
//更改当前页
lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text)-1);
BindRepeter();
}

//排序
protected void btnSort_Click(object sender, EventArgs e)

...{
//判断排序表达式的顺序
if (ViewState["Direction"].ToString() == "DESC")

...{
ViewState["Direction"] = "ASC";
}
else

...{
ViewState["Direction"] = "DESC";
}

/**//*设置完排序的顺序以后,将排序后的dataview
*绑定到repeter上。这个方法,目前有个缺陷,
* 每次排序的时候都是将整个数据源进行排序,
* 并不是对当前的分页进行排序。
*/
BindRepeter();
}