按钮字段删除确认 [英] Button Field Delete Confirmation

查看:84
本文介绍了按钮字段删除确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview中使用了buttonfield。

i想要删除确认。



怎么做。

我试过的代码如下:



i have used buttonfield in gridview.
i want to put delete confirmation on it.

how do this.
My code that i have try is given below.

<asp:GridView ID="GridView1" runat="server" Width="100%"
                  DataKeyNames="NoticeId" onrowdatabound="GridView1_RowDataBound"
                  onrowdeleting="GridView1_RowDeleting" AutoGenerateEditButton="True"
                  onrowediting="GridView1_RowEditing">
                  <Columns>
                      <asp:ButtonField CommandName="Delete" Text="Delete" ButtonType="Link"/>
                      <asp:BoundField />

                  </Columns >
              </asp:GridView>





在gridview的rowdatabound中



in rowdatabound of gridview

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
  foreach (DataControlFieldCell cell in e.Row.Cells)
                    {
                        // check all cells in one row
                        foreach (Control control in cell.Controls)
                        {
                            // Must use LinkButton here instead of ImageButton
                            // if you are having Links (not images) as the command button.
                            ButtonField button = new ButtonField();
                            if (button != null && button.CommandName == "Delete")
                                // Add delete confirmation
                                button.onclientclick= "if (!confirm('Are you sure " +
                                       "you want to delete this record?')) return;";
                        }
                    }
}





但是这里OnclientClck不可用,它给了我错误。

我认为我的代码错了。

这样做的正确方法是什么?



But here OnclientClck Is not avalilable ,and it is giving me error.
I think my code is wrong.
what is the right way to do this?

推荐答案

使用此行作为代码



use this line for your code

button.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");


据我所知,ButtonField不支持OnClientClick属性。



替代解决方案将使用TemplateField定义ASP按钮并按如下方式调用其Onclick事件。



As i understand ButtonField does not support OnClientClick property.

Alternate solution would be to use a TemplateField to define ASP Button and call its Onclick event as follows.

<asp:gridview id="GridView1" runat="server" width="100%" xmlns:asp="#unknown">
                   DataKeyNames="NoticeId" onrowdatabound="GridView1_RowDataBound"
                   onrowdeleting="GridView1_RowDeleting" AutoGenerateEditButton="True"
                   onrowediting="GridView1_RowEditing">
                   <columns>

   <asp:templatefield>
                 <itemtemplate>
                     <asp:button id="Button1" runat="server" text="Delete" commandname="Delete" onclientclick="return ConfirmDeletion();" />
                 </itemtemplate>


                   </asp:templatefield></columns>
               </asp:gridview>









//标题标记中的脚本







//Script in header tag

<script>

function ConfirmDeletion()

{

 return (confirm("Are you sure you want to delete the record?"));

}

<script>


在代码中使用response.write显示javascript >


show javascript using response.write in code

response.write("<script>return confirm('Are You Sure')</script>");


这篇关于按钮字段删除确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆