Javascript确认弹出框太晚了

时间:2012-12-07 21:21:34

标签: javascript asp.net vb.net

我有一个确认操作Javascript弹出框,当用户单击gridview中的操作按钮时,该弹出框应该打开。我的问题是弹出框只有在整个子代码执行完毕后才会打开,并且需要赋值的变量才会获得分配的值,直到为时已晚。如何让弹出窗口打开并允许用户在子结束之前填充DelUserConfirm变量?在此先感谢您的帮助!我的代码如下:

Sub Gridview_RowCommand(ByVal sender As Object,ByVal e As GridViewCommandEventArgs)

    Dim currentcommand As String = e.CommandName
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim cell As GridViewRow = grdUsers.Rows(index)
    Dim currUserID As String = grdUsers.Rows(index).Cells(0).Text

    If e.CommandName = "DelUser" Then

        Dim DelUserConfirm As String = Request.Form("confirm_value")

        If (Not ClientScript.IsStartupScriptRegistered("alert")) Then

            Page.ClientScript.RegisterStartupScript(GetType(action), _
                "alert", "Confirm()", True)

        End If

        If DelUserConfirm = "Yes" Then

            cmd.Connection = conn
            conn.Open()
            cmd.CommandText = "DELETE FROM USERS WHERE USER_NAME = '" + currUserID + "'"
            cmd.ExecuteNonQuery()
            conn.Close()
            dtUsers.Rows(index).Delete()
            grdUsers.DataSource = dtUsers
            grdUsers.DataBind()

        End If

        Exit Sub

    End If

    If e.CommandName = "EditUser" Then

        Session("EditUserPerms") = dtUsers(index)
        Session("UserPerms") = dtUserPerms
        Response.Redirect("AddEditUser.aspx")

    End If

End Sub

添加网络表单代码......

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="UserAdmin.aspx.vb" Inherits="_3rd_party_data_reporting_tool.UserAdmin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Are you sure you want to Delete this user?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<body>
<form id="UserAdmin" runat="server">
<asp:Image ID="imgTRLogo" runat="server" Height="120px" ImageUrl="~/TR_Logo.bmp" Width="120px" style="z-index: 1; left: 5px; top: 5px; position: absolute" />
<asp:Label ID="lblTitle1" runat="server" Text="CA - Global Product Operations" Font-Bold="True" Font-Names="Arial Black" Font-Size="XX-Large" style="z-index: 1; left: 170px; top: 20px; position: absolute"></asp:Label>
<asp:Label ID="lblTitle2" runat="server" Text="Productivity Tools" Font-Bold="True" Font-Names="Arial Black" Font-Size="XX-Large" style="z-index: 1; left: 282px; top: 60px; position: absolute"></asp:Label>
<asp:Label ID="lblTitle3" runat="server" Text="User Administration Panel" Font-Bold="True" Font-Names="Arial Black" Font-Size="Large" style="z-index: 1; left: 312px; top: 135px; position: absolute"></asp:Label>
<asp:GridView ID="grdUsers" runat="server" Font-Names="Arial" autogeneratecolumns="false" style="z-index: 1; left: 28px; top: 184px; position: absolute; height: 144px; width: 731px" OnRowCommand="Gridview_RowCommand" >
<Columns>
<asp:BoundField DataField="USER_NAME" HeaderText="User ID"/>
<asp:BoundField DataField="PW" HeaderText="Password"/>
<asp:BoundField DataField="FIRST_NAME" HeaderText="First Name"/>
<asp:BoundField DataField="LAST_NAME" HeaderText="Last Name"/>
<asp:BoundField DataField="EMAIL" HeaderText="E-mail Address"/>
<asp:BoundField DataField="SAFE_ID" HeaderText="SAFE ID"/>
<asp:BoundField DataField="THIRDPTYRPT" HeaderText="3rd Party Report"/>
<asp:BoundField DataField="EDTK" HeaderText="eDTK Tool"/>
<asp:BoundField DataField="ADMIN" HeaderText="Admin"/>
<asp:buttonfield ButtonType="Button" CommandName="EditUser" HeaderText="Edit User?" Text="Edit" />
<asp:buttonfield ButtonType="Button" CommandName="DelUser" HeaderText="Delete User?" Text="Delete" />
</Columns>
</asp:GridView>
<asp:Button ID="cmdAddNewUser" runat="server" style="z-index: 1; left: 28px; top: 149px; position: absolute; width: 132px" Text="Add New User" />
<asp:Button ID="cmdExit" runat="server" style="position: absolute; top: 22px; left: 729px; height: 45px; width: 54px" Text="Exit" />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

请将ButtonField更改为TemplateField

Delete Confirmation for Delete ButtonField, TemplateField and CommandField in a GridView

<asp:GridView ID="grdUsers" runat="server" ... >
<Columns>
    ...
   <asp:TemplateField>
     <ItemTemplate>
       <asp:Button ID="Button1" runat="server" 
         CommandName="DelUser" 
         OnClientClick="return confirm('Are you sure you want to delete?');" 
         Text="Delete" />
     </ItemTemplate>
   </asp:TemplateField>
  </Columns>
</asp:GridView>