2

I'm trying to attach a Javascript function to confirm a delete of a record in a GridView. I know this can be done more easily using an asp:LinkButton in an ItemTemplate but I am trying to attach it to a CommandField - ShowDeleteButton button.

I've tried to follow this tutorial: display-confirmation-message-on-gridview-deleting

I am new to GridView and my implementation is in VB.Net not C#. Here is the code where I try to inject the Javascript function and I cannot figure out how/why my row.cell references are wrong:

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

Here is my GridView markup snippet (a bit more busy than all bound columns; I count 3 TemplateField items after the first and only BoundField to arrive at the 5th column hence i = 4 above):

<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" /> 

When I ignore the error, the Command buttons are indeed in the 5th column:

When I ignore the error, the Command buttons are indeed in the 5th column.

The error I get without the Try/Catch is: Problem on DirectCast

4

3 回答 3

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" /> 
于 2013-08-15T04:33:31.527 回答
0

只需在 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

谢谢

于 2014-02-13T04:59:58.697 回答
-1

我找到了很多 C# 答案。

页面:

<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
于 2015-02-24T02:48:13.677 回答