一流的商城网站建设,网站关键词扩展,与未成年女生开一间房犯法吗,海关数据查询平台官网在.net2.0中#xff0c;实现对gridview删除行时弹出确认对话框的四种方法 1#xff0c;GridView中如何使用CommandField删除时#xff0c;弹出确认框? 在VS2005提供的GridView中我们可以直接添加一个CommandField删除列#xff1a;asp:CommandField ShowDeleteButton实现对gridview删除行时弹出确认对话框的四种方法 1GridView中如何使用CommandField删除时弹出确认框? 在VS2005提供的GridView中我们可以直接添加一个CommandField删除列asp:CommandField ShowDeleteButtonTrue /完后在它的RowDeleting事件中完成删除。但在多半我们在做这种删除操作时都需要先让操作者再确认下完后再进行删除以避免误操作引起的误删除。可以通过下面方法给GridView删除前加上个确认对话框。首先在GridView的属性对框话框中点击“Columns”进入它的“字段”设计器。接着在“字段”设计器中选择以前已加上的那个CommandField“删除”列这时在它的属性列表下会看到一个“将此它段转换为 TemplateFied”的项点击将它转换为TemplateFied列。完后退出该字段设计器切换到源码视图你会发现该列已由原来的asp:CommandField ShowDeleteButtonTrue /变为了CODE:asp:TemplateField ShowHeaderamp;quot;Falseamp;quot; ItemTemplate asp:LinkButton IDamp;quot;LinkButton1amp;quot; runatamp;quot;serveramp;quot; CausesValidationamp;quot;Falseamp;quot; CommandNameamp;quot;Deleteamp;quot; Textamp;quot;删除amp;quot;/asp:LinkButton/ItemTemplate最后在asp:LinkButton中加入 OnClientClickjavascript:return confirm(真的要删除吗); 或者加入 OnClientClickif(confirm(你确定要删除此记录吗?)){return true;}else{return false;} 这样点击删除时就会先在客户端弹出“确认要删除吗”对话框而原来在RowDeleting事件中写的代码完全不用改变。 注意CommandNamedelete CommandName 一定要设为delete否则将不触发GridView中的RowDeleting 事件 注意在事件protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)里需要设置GridView中的DataKeysName Fid 时,才可以找到相应的ID.Fid为表的主键 id 2 CODE:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType DataControlRowType.DataRow) { if (e.Row.RowIndex -1) { int id Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value); LinkButton lbtnDelete (LinkButton)e.Row.FindControl(amp;quot;lbtnDeleteamp;quot;); if (lbtnDelete ! null) { lbtnDelete.CommandArgument id.ToString(); lbtnDelete.Attributes.Add(amp;quot;onClickamp;quot;, amp;quot;scriptreturn confirm(’是否确认删除’)/scriptamp;quot;); } }}3针对C#中的Windows窗体程序可以先引用System.windwos.Forms,然后在进行处理对于Web应用程序不适合。 CODE:using System.Windows.Forms; protected void gvNewList_RowDeleting(object sender, GridViewDeleteEventArgs e){ DialogResult result MessageBox.Show(amp;quot;确定要删除本行吗amp;quot;, amp;quot;信息提示!amp;quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,MessageBoxOptions.ServiceNotification); if (result DialogResult.Yes) { e.Cancel false; } else { e.Cancel true; }}4添加一个删除列 CODE:protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){ TableCell tc (TableCell)e.Row.Cells[e.Row.Cells.Count - 1]; for (int i 0; i tc.Controls.Count; i 2) { // cerco il controllo ImageButton (ho utilizzato quello) Object o tc.Controls[i]; if (o is ImageButton) { // controllo trovato! // ora aggiungo l’evento js onClick per chiedere conferma all’utente ImageButton lb (ImageButton) o; ((ImageButton)lb).Attributes.Add(amp;quot;onclickamp;quot;, amp;quot;javascript:return confirm(Attenzione: sicuro di voler cancellare?);amp;quot;); } }} -------------------------------------------------------------CODE:asp:TemplateField ShowHeaderamp;quot;Falseamp;quot;ItemStyle HorizontalAlignamp;quot;Centeramp;quot; Widthamp;quot;16pxamp;quot; /ItemTemplateasp:ImageButton IDamp;quot;imgDeleteamp;quot; runatamp;quot;serveramp;quot; CausesValidationamp;quot;Falseamp;quot; CommandNameamp;quot;Deleteamp;quot; ImageUrlamp;quot;~/img/ico_elimina.gifamp;quot; AlternateTextamp;quot;Cancella dataamp;quot; OnClientClickamp;quot;return confirm(’Sicuro di voler cancellare?’);amp;quot; //ItemTemplate/asp:TemplateField 以上方法总结---------Template 方式 -----------------------------------------------CODE:asp:TemplateField ShowHeaderFalseItemTemplateaspinkButton IDLinkButton1 runatserver CausesValidationFalse CommandNameDeleteText删除 OnClientClick’return confirm(Are you sure you want to delete this record?);’/aspinkButton/ItemTemplate/asp:TemplateField -------------RowDeleting method------------------------------------------------ protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){ Response.Write(script languagejavascriptwindow.confirm(确定删除吗)/script);} -------------RowDataBound method--------------------------------------------------------------protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType DataControlRowType.DataRow) { ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add(onclick, javascript:return confirm(’确实要删除该记录吗?’)); } } 另外在web页面中在按钮事件中添加弹出对话框的方法还有, 在前台点击按钮如b1事件为b1_click在后台写b1_click的代码实现点击后弹出“确定要删除吗”选择确定跳转页面如aa.aspx选择取消无动作 protected void Page_Load(object sender, EventArgs e){ b1.Attributes.Add(onclick, javascript:if(confirm(确定要删除吗?)){}else{return false;});}此方法简单方便屡试不爽