之前用gridview控件都是用默认的编辑啊更新等默认功能,今天上网找了些资料,尝试了下用自定义模板实现编辑更新取消删除功能。以下是我最近做的眼镜网后台留言列表,实例图片如下:
点击编辑时出现textbox和checkbox:
以下为各文件的代码,其中红色为应注意地方.蓝色为文件,有理解错误的地方请高手指出,小弟感激不尽
gridview.asp教程x文件
<asp:gridview id="gridview1" runat="server" datakeynames="id" cellpadding="5" width="700px" autogeneratecolumns="false"
onrowdeleting="gridview1_ondeletecommand">
<columns>
<asp:templatefield headertext="">
<itemtemplate>
<asp:button id="delete" runat="server" text="删除" commandname="delete" />
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="用户">
<itemtemplate>
<%# eval("name") %>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="留言">
<itemtemplate>
<%# eval("question") %>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="回复">
<itemtemplate>
<%# eval("answer") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="txtanswer" runat="server" text='<%# eval("answer") %>'></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="审核">
<itemtemplate>
<%# eval("pass") %>
</itemtemplate>
<edititemtemplate>
<asp:checkbox id="pass_check" runat="server" />
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="">
<itemtemplate>
<asp:button runat="server" text="编辑" commandname="edit" />
</itemtemplate>
<edititemtemplate>
<asp:button runat="server" text="更新" commandname="update" />
<asp:button id="cancel" runat="server" text="取消" commandname="cancel" />
</edititemtemplate>
</asp:templatefield>
</columns>
<headerstyle backcolor="#4380cc" forecolor="white" font-names="幼圆" />
<rowstyle backcolor="#eff3fb" horizontalalign="center" font-size="15px" />
<alternatingrowstyle backcolor="#dcdedc" />
<pagerstyle horizontalalign="center" />
</asp:gridview>
mytry.cs文件
1.public datatable getdate(){返回dataset}
2.public void update(string answer,int pass,string pk){} //更新
3.public void delete(string strid){} //删除
gridview.aspx.cs文件
mytry objmytry = new mytry(); //实例化
void binddr()
{
this.gridview1.datasource = objmytry.getdate();
this.gridview1.databind(); //数据绑定
}
protected void gridview1_onrowcommand(object sender, gridviewediteventargs e) //触发编辑事件
{
gridview1.editindex = e.neweditindex; //切换到编辑页
binddr(); //数据绑定
}
protected void gridview1_oncancelcommand(object sender, gridviewcancelediteventargs e)//触发取消事件
{
gridview1.editindex = -1; //后退一页
binddr(); //数据绑定
}
protected void gridview1_onupdatecommand(object sender, gridviewupdateeventargs e) //触发更新事件
{
string pk = gridview1.datakeys[e.rowindex].value.tostring(); //获取当前行主键id
gridviewrow gvr = gridview1.rows[e.rowindex]; //实例化一行
string stranswer = ((textbox)gvr.findcontrol("txtanswer")).text; //获取textbox里面的值
try
{
bool is_checked = ((checkbox)gvr.findcontrol("pass_check")).checked; //获取checkbox的值
if (is_checked == true)
{
int a=convert.toint32(is_checked); //把bool型转为整型
objmytry.update(stranswer, a , pk);
string c = request.urlreferrer.tostring();
response.redirect(c); //返回上次页面
}
else
{
int b = convert.toint32(is_checked);
objmytry.update(stranswer, b , pk);
string c = request.urlreferrer.tostring();
response.redirect(c );
}
}
catch (exception ex)
{
alter(ex.message);
}
}
protected void gridview1_ondeletecommand(object sender, gridviewdeleteeventargs e) //触发删除事件
{
string pk = gridview1.datakeys[e.rowindex].value.tostring();
try
{
objmytry.delete(pk);
alter("删除成功!");
binddr();
}
catch (exception ex)
{
alter(ex.message);
}
}
private void alter(string strmessage) //弹出对话框
{
response.write("<script>alert('" + strmessage + "');</script>");
}