【问题标题】:How to display confirmation message with GridView ShowDeleteButton CommandField如何使用 GridView ShowDeleteButton CommandField 显示确认消息
【发布时间】:2013-08-17 04:20:50
【问题描述】:

我正在尝试附加一个 Javascript 函数来确认删除 GridView 中的记录。我知道在 ItemTemplate 中使用 asp:LinkBut​​ton 可以更轻松地完成此操作,但我正在尝试将其附加到 CommandField - ShowDeleteButton 按钮。

我已尝试按照本教程进行操作:display-confirmation-message-on-gridview-deleting

我是 GridView 的新手,我的实现是在 VB.Net 而不是 C# 中。这是我尝试注入 Javascript 函数的代码,但我无法弄清楚我的 row.cell 引用如何/为什么是错误的:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) _
    Handles GridView1.RowDataBound
    If e.Row.RowState <> DataControlRowState.Edit Then
        If e.Row.RowType = DataControlRowType.DataRow Then  'check for RowType DataRow
            Dim id As String = e.Row.Cells(0).Text      'get the position to be deleted
           Try
            'cast the ShowDeleteButton link to linkbutton
            Dim lb As LinkButton
            Dim i As Integer = 4            'cell we want in the row (relative to 0 not 1) 
            lb = DirectCast(e.Row.Cells(i).Controls(2), LinkButton)
            If lb IsNot Nothing Then        'attach the JavaScript function with the ID as the parameter
                lb.Attributes.Add("onclick", "return ConfirmOnDelete('" & id & "');")
            End If
           Catch ex As Exception
           End Try
        End If
    End If

这是我的 GridView 标记 sn-p(比所有绑定的列都忙;我在第一个也是唯一的 BoundField 之后计算 3 个 TemplateField 项到达第 5 列,因此上面的 i = 4):

<Columns>
    <asp:BoundField DataField="PositionID" HeaderText="ID" ReadOnly="true" />
    <asp:TemplateField HeaderText="PositionTitle">
        <ItemTemplate>
            <%# Eval("PositionTitle")%>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtPositionTitle" Text='<%# Eval("PositionTitle")%>' />
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Incumbent">
        <ItemTemplate>
            <asp:Label ID="lblUser" runat="server" Text='<%# Eval("Incumbent")%>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblUser" runat="server" Text='<%# Eval("Incumbent")%>' Visible = "false"></asp:Label>            
            <asp:DropDownList Width="100%" runat="server" 
               id="ddlUsers" AutoPostBack="true" 
               DataTextField="FullName" DataValueField="UserID"
               OnSelectedIndexChanged="ddlUsers_SelectedIndexChanged">
            </asp:DropDownList> 
        </EditItemTemplate> 
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Backup">
        <ItemTemplate>
            <%#Eval("Backup")%>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblBackup" runat="server" Text='<%# Eval("Backup")%>' Visible = "false"></asp:Label>         
            <asp:DropDownList Width="100%" runat="server" 
               id="ddlUsersBackup" AutoPostBack="true" 
               DataTextField="FullName" DataValueField="UserID"
               OnSelectedIndexChanged="ddlUsersBackup_SelectedIndexChanged">
            </asp:DropDownList> 
        </EditItemTemplate>     
    </asp:TemplateField>

    <asp:CommandField ButtonType="Button" ControlStyle-CssClass="coolbutton" 
        ShowEditButton="true" 
        ShowDeleteButton="true"
        ShowCancelButton="true" /> 

当我忽略错误时,命令按钮确实在第 5 列:

没有 Try/Catch 我得到的错误是:

【问题讨论】:

    标签: asp.net gridview controls linkbutton directcast


    【解决方案1】:

    如果你还没有弄清楚,你的问题在ButtonType="Button"这里:

     <asp:CommandField ButtonType="Button" ControlStyle-CssClass="coolbutton" 
            ShowEditButton="true" 
            ShowDeleteButton="true"
            ShowCancelButton="true" /> 
    

    您不能将命令按钮转换为链接按钮。您必须将其定义为链接按钮(有关 CommandField 类的详细信息,请参阅MSDN)。这是有效的:

    <asp:CommandField ButtonType="Link" ControlStyle-CssClass="coolbutton" 
                ShowEditButton="true" 
                ShowDeleteButton="true"
                ShowCancelButton="true" /> 
    

    【讨论】:

    • 您的回答帮助我以一种新的方式思考这个问题。事实上,我将上面的 ButtonType 更改为 Link 并且 DirectCast 工作。但我不喜欢这种效果。然后,我将 ButtonType 改回 Button 并将 DirectCast 更改为 Button,这一切似乎都工作正常。还有一些测试要做,但感谢您的回答。
    【解决方案2】:

    只需在 onclientclick 事件上调用 javascript 函数并请求确认。如果返回true,则可以调用服务端代码删除。

    下面是解释代码

    <asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton>
    

    下面是javascript函数:

    <script type="text/javascript">
    function fnConfirm() {
        if (confirm("The item will be deleted. Are you sure want to continue?") == true)
            return true;
        else
            return false;
    }
    

    您可以在下面的链接中查看带有源代码的详细文章

    http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html

    谢谢

    【讨论】:

      【解决方案3】:

      我找到了很多 C# 答案。

      aspx 页面:

      <asp:CommandField DeleteText="Delete" ShowDeleteButton="true" 
           ButtonType="Button" />
      

      aspx.vb 页面:

      Protected Sub GridView1_RowDataBound(sender As Object, _
                                       e As GridViewRowEventArgs) _
                                       Handles GridView1.RowDataBound
           If e.Row.RowType = DataControlRowType.DataRow Then
      
              If (e.Row.RowState <> DataControlRowState.Edit) Then
                  Dim oButton As Button
                  oButton = CType(e.Row.Cells(14).Controls(0), Button)
                  oButton.OnClientClick = _
                      "if (confirm(""Are you sure you wish to Delete?"") == false) return;"
              End If
      
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-24
        • 1970-01-01
        • 2021-05-02
        • 1970-01-01
        相关资源
        最近更新 更多