在DataGrid中经弹出窗口确认后执行操作(删除)。(附在DataGrid中修改,添加记录)详见代
2024-07-21 02:16:51
供稿:网友
前台代码:
<asp:label id="message" runat="server"></asp:label><br>
<asp:datagrid id="bigclasslist" width="100%" cellpadding="3" runat="server" datakeyfield="bigclassid" onupdatecommand="mydatagrid_update" oncancelcommand="mydatagrid_cancel" oneditcommand="mydatagrid_edit" ondeletecommand="mydatagrid_del" onsortcommand="mydatagrid_sort" onitemcreated="mydatagrid_itemcreated" autogeneratecolumns="false" allowsorting="true">
<headerstyle font-bold="true" backcolor="gainsboro"></headerstyle>
<columns>
<asp:boundcolumn datafield="bigclassid" readonly="true" headertext="大类编号" sortexpression="bigclassid"></asp:boundcolumn>
<asp:boundcolumn datafield="bigclassname" headertext="大类名称" sortexpression="bigclassname"></asp:boundcolumn>
<asp:boundcolumn datafield="bigclasssortid" headertext="排序编号" sortexpression="bigclasssortid"></asp:boundcolumn>
<asp:editcommandcolumn buttontype="linkbutton" updatetext="更新" canceltext="返回" edittext="编辑"></asp:editcommandcolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:linkbutton id="cmddel" runat="server" text="删除" commandname="delete"></asp:linkbutton>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<table height="23" cellspacing="0" cellpadding="3" width="100%" boder="0">
<tr>
<td bgcolor="#efefef">添加新大类</td>
</tr>
<tr>
<td>类别名称:
<asp:textbox id="bclassname" runat="server"></asp:textbox>
<asp:label id="addmessage" runat="server"></asp:label></td>
</tr>
<tr>
<td>排序编号:
<asp:textbox id="bclassid" runat="server"></asp:textbox></td>
</tr>
<tr>
<td>
<asp:button id="btnaddbclass" runat="server" text="添加大类(a)"></asp:button></td>
</tr>
</table>
后台代码:
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
myconn=new odbcconnection(connstr);
if (!ispostback)
bindgrid("bigclassid");
}
public void bindgrid(string sortfield)
{
// 绑定数据
// odbcdataadapter mydataadapter= new odbcdataadapter("select * from bigclass order by bigclassid desc", myconn);
// dataset ds = new dataset();
// mydataadapter.fill(ds, "bigclass");
// bigclasslist.datasource=ds.tables["bigclass"].defaultview;
// bigclasslist.databind();
odbcdataadapter mydataadapter = new odbcdataadapter("select * from bigclass", myconn);
dataset ds = new dataset();
mydataadapter.fill(ds, "bigclass");
dataview source = ds.tables["bigclass"].defaultview;
source.sort = sortfield;
bigclasslist.datasource=source;
bigclasslist.databind();
}
public void mydatagrid_edit(object sender, datagridcommandeventargs e)
{
// 将当前行置为编辑状态
bigclasslist.edititemindex = (int)e.item.itemindex;
bindgrid("bigclassid");
}
public void mydatagrid_cancel(object sender, datagridcommandeventargs e)
{
// 将当前行置为非编辑状态
bigclasslist.edititemindex = -1;
bindgrid("bigclassid");
}
public void mydatagrid_update(object sender, datagridcommandeventargs e)
{
// 更新当前行
string bigclassid=e.item.cells[0].text;
string bigclassname=regex.replace(((textbox)e.item.cells[1].controls[0]).text,"'","''");
string bigclasssortid=((textbox)e.item.cells[2].controls[0]).text;
string updatecmd = "update bigclass set bigclassname ='" + bigclassname + "',"
+ "bigclasssortid=" + bigclasssortid
+ " where bigclassid =" + bigclassid;
mycomm = new odbccommand(updatecmd, myconn);
mycomm.connection.open();
try
{
mycomm.executenonquery();
// response.write("<font color=red><b>已更新记录</b><font><br>");
message.text = "<font color=red><b>已更新记录</b><font><br>";
bigclasslist.edititemindex = -1;
}
catch (odbcexception exc)
{
message.text="<font color=red><b>错误:" + exc.message.tostring() + "</b></font>";
}
mycomm.connection.close();
bindgrid("bigclassid");
}
public void mydatagrid_itemcreated(object sender, datagriditemeventargs e)
{
// 删除大类
linkbutton l=new linkbutton();
switch(e.item.itemtype)
{
case(listitemtype.item):
l=(system.web.ui.webcontrols.linkbutton)e.item.findcontrol("cmddel");
l.attributes.add("onclick","return confirm('确定要删除该大类吗?');");
break;
case(listitemtype.alternatingitem):
l=(system.web.ui.webcontrols.linkbutton)e.item.findcontrol("cmddel");
l.attributes.add("onclick","return confirm('确定要删除该大类吗?');");
break;
}
}
protected void mydatagrid_del(object sender, datagridcommandeventargs e)
{
myconn=new odbcconnection(connstr);
myconn.open();
string bigclassid=e.item.cells[0].text;;
string deletecmd = "delete from bigclass where bigclassid =" + bigclassid;
mycomm=new odbccommand(deletecmd,myconn);
mycomm.executenonquery();
bindgrid("bigclassid");
}
protected void mydatagrid_sort(object sender, datagridsortcommandeventargs e)
{
bindgrid(e.sortexpression);
}
private void btnaddbclass_click(object sender, system.eventargs e)
{
string bclassname=regex.replace(bclassname.text,"'","''");
string bclassid=bclassid.text;
if(bclassname!="" & bclassid!="")
{
myconn=new odbcconnection(connstr);
myconn.open();
string insertinto="insert into bigclass (bigclassname,bigclasssortid) values ("
+ "'" + bclassname + "',"
+ bclassid + ")";
// response.write(insertinto);
mycomm=new odbccommand(insertinto,myconn);
mycomm.executenonquery();
bclassname.text="";
bclassid.text="";
addmessage.text="<font color=red><b>已经添加新的文章大类!</b></font>";
bindgrid("bigclassid");
}
else
{
addmessage.text="<font color=red><b>请填写完整!</b></font>";
}
}