近日在博客园看到一篇转载的文章《Repeater实现GridView修改删除功能》,感觉作者的思路很好(在不考虑性能的情况下);
在Repeater的里面使用两个Panel来实现交互性的Edit和非Edit状态。
原文下载地址:http://www.x-wow.net/wow/Download/RepeaterTest.rar 不知道是否还能下载,没有测试过。
有兴趣的可以下载下去看看。粘出来供大家参考一下。
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterTest.aspx.cs" Inherits="wow_RepeaterTest" %>
<!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>Repeater实现GridView编辑修改模式</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center;">
<table border="1" cellpadding="0" cellspacing="1" style="width: 80%; border: 1px solid #ccc; font-size: 12px;">
<tr>
<td align="center">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"
OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="1" style="background-color: #CCC;
width: 80%;">
<tr>
<td style="background-color: #FFF; height: 25px;">
A标题
</td>
<td style="background-color: #FFF;">
B标题
</td>
<td style="background-color: #FFF;">
C标题
</td>
<td style="background-color: #FFF;">
编辑
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr>
<td style="background-color: #FFF; height: 25px; width: 30%;">
<%# DataBinder.Eval(Container.DataItem, "a")%>
</td>
<td style="background-color: #FFF; width: 30%;">
<%# DataBinder.Eval(Container.DataItem,"b") %>
</td>
<td style="background-color: #FFF; width: 30%;">
<%# DataBinder.Eval(Container.DataItem,"c") %>
</td>
<td style="background-color: #FFF; width: 10%;">
<asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CommandName="Edit" Text="编辑"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CommandName="Delete" Text="删除"></asp:LinkButton>
</td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr>
<td style="background-color: #FFF; height: 25px; width: 30%;">
<asp:TextBox ID="txtA" Text='<%# DataBinder.Eval(Container.DataItem,"a") %>' runat="server"></asp:TextBox>
</td>
<td style="background-color: #FFF; width: 30%;">
<asp:TextBox ID="txtB" Text='<%# DataBinder.Eval(Container.DataItem,"b") %>' runat="server"></asp:TextBox>
</td>
<td style="background-color: #FFF; width: 30%;">
<asp:TextBox ID="txtC" Text='<%# DataBinder.Eval(Container.DataItem,"c") %>' runat="server"></asp:TextBox>
</td>
<td style="background-color: #FFF; width: 80px; width: 10%;">
<asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CommandName="Cancel" Text="取消"></asp:LinkButton>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td align="center" style="height:80px;">
点<a href="http://www.x-wow.net/wow/Download/RepeaterTest.rar" title="源代码" style="color: Red;">这里</a>示例下载
</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.Text;
public partial class wow_RepeaterTest : System.Web.UI.Page
{
DataTable m_dt = new DataTable();
int m_iID = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Repeater1.DataSource = this.InitDataTable();
Repeater1.DataBind();
}
}
protected DataTable InitDataTable()
{
m_dt.Columns.Add("ID");
m_dt.Columns.Add("a");
m_dt.Columns.Add("b");
m_dt.Columns.Add("c");
DataRow dr;
for (int i = 1; i <= 10; i++)
{
dr = m_dt.NewRow();
dr["ID"] = i.ToString();
dr["a"] = "A" + ((i * 10).ToString());
dr["b"] = "B" + ((i * 100).ToString());
dr["c"] = "C" + ((i * 1000).ToString());
m_dt.Rows.Add(dr);
}
return m_dt;
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (m_dt.Rows[e.Item.ItemIndex]["ID"].ToString() != m_iID.ToString())
{
((Panel)e.Item.FindControl("plItem")).Visible = true;
((Panel)e.Item.FindControl("plEdit")).Visible = false;
}
else
{
((Panel)e.Item.FindControl("plItem")).Visible = false;
((Panel)e.Item.FindControl("plEdit")).Visible = true;
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
m_iID = int.Parse(e.CommandArgument.ToString());
}
else if (e.CommandName == "Cancel")
{
m_iID = -1;
}
else if (e.CommandName == "Update")
{
//Update.......
string sA = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtA")).Text.Trim();
string sB = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtB")).Text.Trim();
string sC = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtC")).Text.Trim();
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" +
e.CommandArgument + ";页面值:A," + sA + "----B," + sB + "----C," + sC + "');", true);
}
else if (e.CommandName == "Delete")
{
//Delete.......
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
}
Repeater1.DataSource = this.InitDataTable();
Repeater1.DataBind();
}
}
针对数据列少并且数据量不大的情况下使用还是很好 的,否则慎用之!!!