2012-12-07 48 views
0

我有一個確認操作Javascript彈出框,應該在用戶點擊GridView中的操作按鈕時打開。我的問題是,彈出框不會打開,直到整個子代碼已經執行完畢,並且需要分配值的變量沒有得到賦值,直到它爲時已晚。如何讓彈出窗口打開並允許用戶在子結束之前填充DelUserConfirm變量?先謝謝您的幫助!我的代碼如下:Javascript確認彈出框太晚

子Gridview_RowCommand(BYVAL發件人爲對象,BYVALË作爲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 

添加Web窗體代碼...

<%@ 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> 
+0

你能提供WebForm中的代碼?你的確認框如何? –

+0

你可以在按鈕本身上放一個OnClientClick =「」標籤嗎?否則,RegisterStartupScript只是在頁面的其餘部分之後加載javascript。 –

+0

您意識到.NET代碼在服務器上運行,並且確認在客戶端上運行,並且在用戶提交表單或單擊鏈接之前兩者之間沒有連接。 –

回答