如何在详细视图中为命令字段添加删除确认提示?

Posted

技术标签:

【中文标题】如何在详细视图中为命令字段添加删除确认提示?【英文标题】:how to add delete confirmation prompt for command field in detail view? 【发布时间】:2012-03-08 05:30:47 【问题描述】:

我想在用户尝试删除详细视图中的记录时提示用户确认?我有命令文件,其中 showDeletebutton 设置为 true。

我找到了如何对gridview进行确认,但是如何修改以匹配详细视图?

代码:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)

    // loop all data rows
    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.
            ImageButton button = control as ImageButton;
            if (button != null && button.CommandName == "Delete")
                // Add delete confirmation
                button.OnClientClick = "if (!confirm('Are you sure " + 
                       "you want to delete this record?')) return;";
        
    


有人吗?

【问题讨论】:

【参考方案1】:
     <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        .....
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:TemplateField ShowHeader="False">
                 <ItemTemplate>
                    <asp:LinkButton  ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="New" Text="New"></asp:LinkButton>

                    <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>

                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView

这可以在标记代码上轻松完成。我只是将js代码添加到删除按钮的onClientClick属性中:

OnClientClick="return confirm('Are you sure you want to delete this record');"

或者如果你想在后面的代码中这样做:

 protected void DetailsView1_DataBound(object sender, EventArgs e)
  
    LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
    bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
  

【讨论】:

那个 TemplateField 从哪里开始? 当然,我故意省略了部分代码,但是最后一个TemplateField和ItemTemplate的开头可以放在第一个链接按钮之前。查看更新【参考方案2】:

我找到了我的问题的答案。

我的回答:

 protected void DViewComputer_DataBound1(object sender, EventArgs e)

    int noRow = DViewComputer.Rows.Count - 1;//get the no of record

    if (noRow >0)
    
        Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);

        // Add delete confirmation
        ((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
                               "you want to delete this record?')) return;";

    

无论如何,谢谢你们的帮助。

【讨论】:

只是补充一点,有一个 jQuery 解决方案可能会让一些人对this question的答案感兴趣 不起作用。当用户在编辑或插入模式下单击取消时,此解决方案将要求用户确认他们要删除此记录。【参考方案3】:
   foreach (Control control in cell.Controls)
   
        // Must use LinkButton here instead of ImageButton
        // if you are having Links (not images) as the command button.
        ImageButton button = control as ImageButton;
        if (button != null && button.CommandName == "Delete")
            // Add delete confirmation
            button.Attributes.Add("onclick","your javascript here");
    

【讨论】:

【参考方案4】:

请看以下网址......http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com

【讨论】:

【参考方案5】:

这更正了 OP 的解决方案。该代码是从此处找到的代码翻译而来的:http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html

protected void dvEvent_DataBound(object sender, EventArgs e)


    int commandRowIndex = dvEvent.Rows.Count - 1;
    if (commandRowIndex > 0)
    
        DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
        DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];

        foreach (Control ctrl in cell.Controls)
        
            if (ctrl is ImageButton)
            
                ImageButton ibt = (ImageButton)ctrl;
                if (ibt.CommandName == "Delete")
                
                    ibt.ToolTip = "Click here to Delete";
                    ibt.CommandName = "Delete";
                    ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
                                "you want to delete this record?')) return;";
                
            
        
    

【讨论】:

以上是关于如何在详细视图中为命令字段添加删除确认提示?的主要内容,如果未能解决你的问题,请参考以下文章

Mysql数据表字段的增,改,删

SENCHA:如何通过单击 SENCHA touch 中的添加/删除按钮动态添加/删除文本字段

3-2组管理-添加组/删除组/确认组信息

Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号

如何在“详细”实体视图中为“主/父”实体“保存”(核心数据)?

Linux_019_删除rm