GridView自定义删除操作的具体方法
程序员文章站
2024-02-28 12:37:22
首先,我们前端的代码如下:
复制代码 代码如下:
<asp:gridview id="gridviewdxjk" cssclass="gridview" runat="server" allowpaging="true"
datakeynames="p_id" autogeneratecolumns="false"
rowstyle-horizontalalign="center" borderwidth="1px" pagesize="17"
onrowdeleting="gridviewdxjk_rowdeleting"
onrowdatabound="gridviewdxjk_rowdatabound"
onpageindexchanging="gridviewdxjk_pageindexchanging" >
<headerstyle cssclass="head" />
<pagerstyle cssclass="pager" />
<rowstyle cssclass="row" />
<editrowstyle cssclass="editrow" />
<alternatingrowstyle cssclass="altrow" />
<emptydatarowstyle cssclass="empty" />
<columns>
<asp:hyperlinkfield headertext="编辑" controlstyle-width="50" datanavigateurlfields="p_id" datanavigateurlformatstring="smsmodify.aspx?id={0}" text="编辑" >
<controlstyle width="50px"></controlstyle></asp:hyperlinkfield>
<asp:commandfield showdeletebutton="true" deletetext="删除" >
<controlstyle width="50px"></controlstyle></asp:commandfield>
<asp:boundfield datafield="p_id" headertext="id" sortexpression="p_id" itemstyle-horizontalalign="center" visible="false" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_name" headertext="名称" sortexpression="p_name" />
<asp:boundfield datafield="p_type" headertext="通知方式" sortexpression="p_type" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_fzr" headertext="姓名" sortexpression="p_fzr" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_tel" headertext="通知手机" sortexpression="p_tel" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_jg" headertext="通知间隔(小时)" sortexpression="p_jg" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_on" headertext="是否开启" sortexpression="p_on" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_lasttime" headertext="最后发送时间" sortexpression="p_lasttime" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_memo" headertext="备注" sortexpression="p_memo" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
</columns>
<emptydatatemplate>
没有数据!
</emptydatatemplate>
<pagertemplate>
<table width="100%" class="gvpage" style="font-size:12px;">
<tr>
<td style="text-align: right">
第<asp:label id="lblpageindex" runat="server" text='<%# ((gridview)container.parent.parent).pageindex + 1 %>'></asp:label>页
/共<asp:label id="lblpagecount" runat="server" text='<%# ((gridview)container.parent.parent).pagecount %>'></asp:label>页
<asp:linkbutton id="linkbuttonfirstpage" runat="server" commandargument="first" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != 0 %>">首页</asp:linkbutton>
<asp:linkbutton id="linkbuttonpreviouspage" runat="server" commandargument="prev" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != 0 %>">上一页</asp:linkbutton>
<asp:linkbutton id="linkbuttonnextpage" runat="server" commandargument="next" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>">下一页</asp:linkbutton>
<asp:linkbutton id="linkbuttonlastpage" runat="server" commandargument="last" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>">尾页</asp:linkbutton>
<asp:textbox id="txtnewpageindex" runat="server" text='<%# ((gridview)container.parent.parent).pageindex + 1 %>' width="20px" autopostback="true" ></asp:textbox>
<asp:linkbutton id="btngoex" runat="server" commandargument="go" commandname="page" text="go" onclick="btngoex_click"></asp:linkbutton>
</td>
</tr>
</table>
</pagertemplate>
</asp:gridview>
后端的话,由于需要弹出删除前的确认框,所以,我们需要在rowdatabound里面做点什么?同时,要想真正的删除,还需要触发rowdeleting事件,具体代码如下:
//报警删除
protected void gridviewdxjk_rowdeleting(object sender, gridviewdeleteeventargs e)
{
string key = gridviewdxjk.datakeys[e.rowindex].value.tostring();
bool flag = bll.delete(int32.parse(key));
if (flag)
nxt_wlservice.app_code.jscript.alert("删除成功!", this);
else
nxt_wlservice.app_code.jscript.alert("删除失败!", this);
}
首先,我们前端的代码如下:
复制代码 代码如下:
<asp:gridview id="gridviewdxjk" cssclass="gridview" runat="server" allowpaging="true"
datakeynames="p_id" autogeneratecolumns="false"
rowstyle-horizontalalign="center" borderwidth="1px" pagesize="17"
onrowdeleting="gridviewdxjk_rowdeleting"
onrowdatabound="gridviewdxjk_rowdatabound"
onpageindexchanging="gridviewdxjk_pageindexchanging" >
<headerstyle cssclass="head" />
<pagerstyle cssclass="pager" />
<rowstyle cssclass="row" />
<editrowstyle cssclass="editrow" />
<alternatingrowstyle cssclass="altrow" />
<emptydatarowstyle cssclass="empty" />
<columns>
<asp:hyperlinkfield headertext="编辑" controlstyle-width="50" datanavigateurlfields="p_id" datanavigateurlformatstring="smsmodify.aspx?id={0}" text="编辑" >
<controlstyle width="50px"></controlstyle></asp:hyperlinkfield>
<asp:commandfield showdeletebutton="true" deletetext="删除" >
<controlstyle width="50px"></controlstyle></asp:commandfield>
<asp:boundfield datafield="p_id" headertext="id" sortexpression="p_id" itemstyle-horizontalalign="center" visible="false" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_name" headertext="名称" sortexpression="p_name" />
<asp:boundfield datafield="p_type" headertext="通知方式" sortexpression="p_type" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_fzr" headertext="姓名" sortexpression="p_fzr" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_tel" headertext="通知手机" sortexpression="p_tel" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_jg" headertext="通知间隔(小时)" sortexpression="p_jg" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_on" headertext="是否开启" sortexpression="p_on" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_lasttime" headertext="最后发送时间" sortexpression="p_lasttime" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
<asp:boundfield datafield="p_memo" headertext="备注" sortexpression="p_memo" itemstyle-horizontalalign="center" >
<itemstyle horizontalalign="center"></itemstyle></asp:boundfield>
</columns>
<emptydatatemplate>
没有数据!
</emptydatatemplate>
<pagertemplate>
<table width="100%" class="gvpage" style="font-size:12px;">
<tr>
<td style="text-align: right">
第<asp:label id="lblpageindex" runat="server" text='<%# ((gridview)container.parent.parent).pageindex + 1 %>'></asp:label>页
/共<asp:label id="lblpagecount" runat="server" text='<%# ((gridview)container.parent.parent).pagecount %>'></asp:label>页
<asp:linkbutton id="linkbuttonfirstpage" runat="server" commandargument="first" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != 0 %>">首页</asp:linkbutton>
<asp:linkbutton id="linkbuttonpreviouspage" runat="server" commandargument="prev" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != 0 %>">上一页</asp:linkbutton>
<asp:linkbutton id="linkbuttonnextpage" runat="server" commandargument="next" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>">下一页</asp:linkbutton>
<asp:linkbutton id="linkbuttonlastpage" runat="server" commandargument="last" commandname="page" visible="<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>">尾页</asp:linkbutton>
<asp:textbox id="txtnewpageindex" runat="server" text='<%# ((gridview)container.parent.parent).pageindex + 1 %>' width="20px" autopostback="true" ></asp:textbox>
<asp:linkbutton id="btngoex" runat="server" commandargument="go" commandname="page" text="go" onclick="btngoex_click"></asp:linkbutton>
</td>
</tr>
</table>
</pagertemplate>
</asp:gridview>
后端的话,由于需要弹出删除前的确认框,所以,我们需要在rowdatabound里面做点什么?同时,要想真正的删除,还需要触发rowdeleting事件,具体代码如下:
复制代码 代码如下:
//报警删除
protected void gridviewdxjk_rowdeleting(object sender, gridviewdeleteeventargs e)
{
string key = gridviewdxjk.datakeys[e.rowindex].value.tostring();
bool flag = bll.delete(int32.parse(key));
if (flag)
nxt_wlservice.app_code.jscript.alert("删除成功!", this);
else
nxt_wlservice.app_code.jscript.alert("删除失败!", this);
}
protected void gridviewdxjk_rowdatabound(object sender, gridviewroweventargs e)
{
if (e.row.rowtype == datacontrolrowtype.datarow)
{
linkbutton btn = (linkbutton)e.row.cells[1].controls[0];
if (btn.text.equals("删除"))//刪除鈕才加提示訊息
btn.onclientclick = "if (confirm('你确认要删除?')) javascript:__dopostback('gridviewdxjk','delete$" + e.row.rowindex.tostring() + "'); else return false;";
}
}