Repeater控件实现编辑、更新、删除等操作示例代码
程序员文章站
2024-02-25 23:01:27
如何在repeater控件中实现像gridview控件一样的编辑、更新、删除功能? 复制代码 代码如下: protected void page_load(object s...
如何在repeater控件中实现像gridview控件一样的编辑、更新、删除功能?
protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
bindgrid();
}
}
private void bindgrid()
{
string strsql = "select * from [user]";
oledbconnection objconnection = new oledbconnection(getstrconnection());
objconnection.open();
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
oledbdatareader reader = objcommand.executereader(commandbehavior.closeconnection);
rptuser.datasource = reader;
rptuser.databind();
}
protected void rptuser_itemdatabound(object sender, repeateritemeventargs e)
{
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
system.data.common.dbdatarecord record = (system.data.common.dbdatarecord)e.item.dataitem;
int userid = int.parse(record["userid"].tostring());
if (userid != id)
{
((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 rptuser_itemcommand(object source, repeatercommandeventargs e)
{
if (e.commandname == "edit")
{
id = int.parse(e.commandargument.tostring());
}
else if (e.commandname == "cancel")
{
id = -1;
}
else if (e.commandname == "update")
{
string name = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtname")).text.trim();
string email = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtemail")).text.trim();
string qq = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtqq")).text.trim();
string strsql = "update [user] set name=@name,email=@email,qq=@qq where userid=@userid";
oledbconnection objconnection = new oledbconnection(getstrconnection());
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
objcommand.parameters.add("@name", oledbtype.varwchar);
objcommand.parameters["@name"].value = name;
objcommand.parameters.add("@email", oledbtype.varwchar);
objcommand.parameters["@email"].value = email;
objcommand.parameters.add("@qq", oledbtype.varwchar);
objcommand.parameters["@qq"].value = qq;
objcommand.parameters.add("@userid", oledbtype.integer);
objcommand.parameters["@userid"].value = int.parse(e.commandargument.tostring());
objconnection.open();
objcommand.executenonquery();
objconnection.close();
}
else if (e.commandname == "delete")
{
string strsql = "delete * from [user] where userid=@userid";
oledbconnection objconnection = new oledbconnection(getstrconnection());
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
objcommand.parameters.add("@userid", oledbtype.integer);
objcommand.parameters["@userid"].value = int.parse(e.commandargument.tostring());
objconnection.open();
objcommand.executenonquery();
objconnection.close();
}
bindgrid();
}
private string getstrconnection()
{
return "provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("~/database/test.mdb");
}
<</code>form id="form1" runat="server">
<</code>asp:repeater id="rptuser" runat="server" onitemcommand="rptuser_itemcommand"
onitemdatabound="rptuser_itemdatabound">
<</code>headertemplate>
<</code>table width="960" align="center" cellpadding="3" cellspacing="1" style="background-color: #ccc;">
<</code>thead style="background-color: #eee;">
<</code>tr>
<</code>th width="10%">
用户id
</</code>th>
<</code>th>
用户名
</</code>th>
<</code>th width="22%">
邮件
</</code>th>
<</code>th width="20%">
qq
</</code>th>
<</code>th width="15%">
注册时间
</</code>th>
<</code>th width="12%">
操作
</</code>th>
</</code>tr>
</</code>thead>
<</code>tbody style="background-color: #fff;">
</</code>headertemplate>
<</code>itemtemplate>
<</code>asp:panel id="plitem" runat="server">
<</code>tr style="text-align: center;">
<</code>td>
复制代码 代码如下:
protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
bindgrid();
}
}
private void bindgrid()
{
string strsql = "select * from [user]";
oledbconnection objconnection = new oledbconnection(getstrconnection());
objconnection.open();
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
oledbdatareader reader = objcommand.executereader(commandbehavior.closeconnection);
rptuser.datasource = reader;
rptuser.databind();
}
protected void rptuser_itemdatabound(object sender, repeateritemeventargs e)
{
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
system.data.common.dbdatarecord record = (system.data.common.dbdatarecord)e.item.dataitem;
int userid = int.parse(record["userid"].tostring());
if (userid != id)
{
((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 rptuser_itemcommand(object source, repeatercommandeventargs e)
{
if (e.commandname == "edit")
{
id = int.parse(e.commandargument.tostring());
}
else if (e.commandname == "cancel")
{
id = -1;
}
else if (e.commandname == "update")
{
string name = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtname")).text.trim();
string email = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtemail")).text.trim();
string qq = ((textbox)this.rptuser.items[e.item.itemindex].findcontrol("txtqq")).text.trim();
string strsql = "update [user] set name=@name,email=@email,qq=@qq where userid=@userid";
oledbconnection objconnection = new oledbconnection(getstrconnection());
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
objcommand.parameters.add("@name", oledbtype.varwchar);
objcommand.parameters["@name"].value = name;
objcommand.parameters.add("@email", oledbtype.varwchar);
objcommand.parameters["@email"].value = email;
objcommand.parameters.add("@qq", oledbtype.varwchar);
objcommand.parameters["@qq"].value = qq;
objcommand.parameters.add("@userid", oledbtype.integer);
objcommand.parameters["@userid"].value = int.parse(e.commandargument.tostring());
objconnection.open();
objcommand.executenonquery();
objconnection.close();
}
else if (e.commandname == "delete")
{
string strsql = "delete * from [user] where userid=@userid";
oledbconnection objconnection = new oledbconnection(getstrconnection());
oledbcommand objcommand = new oledbcommand(strsql, objconnection);
objcommand.parameters.add("@userid", oledbtype.integer);
objcommand.parameters["@userid"].value = int.parse(e.commandargument.tostring());
objconnection.open();
objcommand.executenonquery();
objconnection.close();
}
bindgrid();
}
private string getstrconnection()
{
return "provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("~/database/test.mdb");
}
<</code>form id="form1" runat="server">
<</code>asp:repeater id="rptuser" runat="server" onitemcommand="rptuser_itemcommand"
onitemdatabound="rptuser_itemdatabound">
<</code>headertemplate>
<</code>table width="960" align="center" cellpadding="3" cellspacing="1" style="background-color: #ccc;">
<</code>thead style="background-color: #eee;">
<</code>tr>
<</code>th width="10%">
用户id
</</code>th>
<</code>th>
用户名
</</code>th>
<</code>th width="22%">
邮件
</</code>th>
<</code>th width="20%">
</</code>th>
<</code>th width="15%">
注册时间
</</code>th>
<</code>th width="12%">
操作
</</code>th>
</</code>tr>
</</code>thead>
<</code>tbody style="background-color: #fff;">
</</code>headertemplate>
<</code>itemtemplate>
<</code>asp:panel id="plitem" runat="server">
<</code>tr style="text-align: center;">
<</code>td>