源码使用常见问题 技术服务淘宝店 源码环境搭建和部署视频! 快速通道:[源码发布] | [实名认证] | [专家面对面]

在.net2.0中,实现对gridview删除行时弹出确认对话框的四种方法

[ 9719 查看 / 3 回复 ]

非实名用户所发信息,请自行甄别真伪非实名,信息自行甄别

1,GridView中如何使用CommandField删除时,弹出确认框?  在VS2005提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" />,完后在它的RowDeleting事件中完成删除。但在多半我们在做这种删除操作时都需要先让操作者再确认下,完后再进行删除,以避免误操作引起的误删除。 可以通过下面方法给GridView删除前加上个确认对话框。 首先,在GridView的属性对框话框中点击“Columns”进入它的“字段”设计器。接着在“字段”设计器中选择以前已加上的那个CommandField“删除”列,这时在它的属性列表下会看到一个“将此它段转换为 TemplateFied”的项,点击将它转换为TemplateFied列。 完后退出该字段设计器,切换到源码视图你会发现该列已由原来的:<asp:CommandField ShowDeleteButton="True" /> 变为了:
  1. <asp:TemplateField ShowHeader=&amp;quot;False&amp;quot;>
  2.                                   <ItemTemplate>
  3.                                       <asp:LinkButton ID=&amp;quot;LinkButton1&amp;quot; runat=&amp;quot;server&amp;quot; CausesValidation=&amp;quot;False&amp;quot; CommandName=&amp;quot;Delete&amp;quot;    Text=&amp;quot;删除&amp;quot;></asp:LinkButton>
  4. </ItemTemplate>
  5. 最后在<asp:LinkButton>中加入:OnClientClick=&amp;quot;return confirm(’确认要删除吗?’);&amp;quot; 
复制代码
这样点击删除时就会先在客户端弹出“确认要删除吗?”对话框,而原来在RowDeleting事件中写的代码完全不用改变。 2,
  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. if (e.Row.RowType == DataControlRowType.DataRow)
  4. {
  5. if (e.Row.RowIndex > -1)
  6. {
  7. int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
  8. LinkButton lbtnDelete = (LinkButton)e.Row.FindControl(&amp;quot;lbtnDelete&amp;quot;);
  9. if (lbtnDelete != null)
  10. {
  11. lbtnDelete.CommandArgument = id.ToString();
  12. lbtnDelete.Attributes.Add(&amp;quot;onClick&amp;quot;, &amp;quot;<script>return confirm(’是否确认删除!’)</script>&amp;quot;);
  13. }
  14. }
  15. }

  16. }
复制代码
3,先引用System.windwos.Forms,然后在进行处理
  1. using System.Windows.Forms;

  2. protected void gvNewList_RowDeleting(object sender, GridViewDeleteEventArgs e)
  3. {
  4. DialogResult result = MessageBox.Show(&amp;quot;确定要删除本行吗?&amp;quot;, &amp;quot;信息提示!&amp;quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,MessageBoxOptions.ServiceNotification);
  5. if (result == DialogResult.Yes)
  6. {
  7. e.Cancel = false;
  8. }
  9. else
  10. {
  11. e.Cancel = true;
  12. }
  13. }
复制代码
4,添加一个删除列
  1. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  2. {
  3. TableCell tc = (TableCell)e.Row.Cells[e.Row.Cells.Count - 1];
  4. for (int i = 0; i < tc.Controls.Count; i += 2)
  5. {
  6. // cerco il controllo ImageButton (ho utilizzato quello)
  7. Object o = tc.Controls[i];
  8. if (o is ImageButton)
  9. {
  10. // controllo trovato!
  11. // ora aggiungo l’evento js onClick per chiedere conferma all’utente
  12. ImageButton lb = (ImageButton) o;
  13. ((ImageButton)lb).Attributes.Add(&amp;quot;onclick&amp;quot;, @&amp;quot;javascript:return confirm(’Attenzione: sicuro di voler cancellare?’);&amp;quot;);
  14. }
  15. }
  16. }
复制代码
-------------------------------------------------------------
  1. <asp:TemplateField ShowHeader=&amp;quot;False&amp;quot;>
  2. <ItemStyle HorizontalAlign=&amp;quot;Center&amp;quot; Width=&amp;quot;16px&amp;quot; />
  3. <ItemTemplate>
  4. <asp:ImageButton ID=&amp;quot;imgDelete&amp;quot; runat=&amp;quot;server&amp;quot; CausesValidation=&amp;quot;False&amp;quot; CommandName=&amp;quot;Delete&amp;quot; ImageUrl=&amp;quot;~/img/ico_elimina.gif&amp;quot; AlternateText=&amp;quot;Cancella data&amp;quot; OnClientClick=&amp;quot;return confirm(’Sicuro di voler cancellare?’);&amp;quot; />
  5. </ItemTemplate>
  6. </asp:TemplateField>
复制代码
以上方法总结 ---------Template 方式 51aspx.com-----------------------------------------------
  1. <asp:TemplateField ShowHeader="False">
  2. <ItemTemplate>
  3. <aspinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
  4. Text="删除" OnClientClick=’return confirm("Are you sure you want to delete this record?");’></aspinkButton>
  5. </ItemTemplate>
  6. </asp:TemplateField>
复制代码
-------------RowDeleting method------------------------------------------------ protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Response.Write("<script>window.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(’确实要删除该记录吗?’)"); }
最后编辑51aspx 最后编辑于 2007-11-29 19:19:34
分享 转发
广告位招租,我换新头像了阿
TOP

回复:在.net2.0中,实现对gridview删除行时弹出确认对话框的四种方法

非实名用户所发信息,请自行甄别真伪非实名,信息自行甄别

沙发~~~~~
TOP

回复:在.net2.0中,实现对gridview删除行时弹出确认对话框的四种方法

非实名用户所发信息,请自行甄别真伪非实名,信息自行甄别

楼主要是能给完全的代码就好了,我们这种初学者,根本不知道这些代码放在哪里啊?
TOP

非实名用户所发信息,请自行甄别真伪非实名,信息自行甄别

哈哈! 太好啦! 今天又学到知识啦! 谢谢版主!
TOP