删除记录前的密码窗口

时间:2017-01-09 20:16:09

标签: c# asp.net

我使用以下代码从数据库中删除记录。

protected void lnkDelete_Click(object sender, EventArgs e)
{
    string id = ((sender as LinkButton).CommandArgument).ToString();
    string constr = ConfigurationManager.ConnectionStrings["..."].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "Delete from UploadedFile where FileID=@FileID";
            cmd.Parameters.AddWithValue("@FileID", id);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}

删除链接按钮将删除记录。当用户点击删除按钮并要求他们输入密码时,我想添加一个弹出窗口,如果密码正确则删除,否则他们无法删除记录。我怎么能用JavaScript或代码隐藏呢?

1 个答案:

答案 0 :(得分:0)

在asp按钮 OnClientClick

中添加属性

ASP代码

<asp:button id="Button1" usesubmitbehavior="true" text="Delete" onclientclick="Confirm()" runat="server" onclick="Button1_Click" />

<强>脚本

 <script>
      function Confirm(){
           // Write Your Code here For Display a Div and Confirm password
      }
 </script>

另一种认证方式

* .aspx(设计文件)

添加简单的Div

<div id="Auth" runat="Server" style="visibility:hidden;">  
   <p>Enter Password</p>
   <input type="Password" runat="Server" id="Pass"/>
   <asp:button runat="Server" id="btnAuth" onclick="btnAuth_Click" text="Auth"/>
</div>

***。cs文件**

为btnAuth按钮创建一个事件

void btnAuth_Click(Object sender,EventArgs e){
    string Password = Pass.value;
    bool Success = false;
    // Check Password here

    if(Success){
        DeleteRecord(this.DeleteObject);
    }

}

创建一个运行删除命令的函数

public void DeleteRecord(object _deleteObject){

    string id = ((_deleteObject as LinkButton).CommandArgument).ToString();
    string constr = ConfigurationManager.ConnectionStrings["..."].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "Delete from UploadedFile where FileID=@FileID";
            cmd.Parameters.AddWithValue("@FileID", id);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);    
  }

创建全局变量名DeleteObject

 //Global Linkbutton object set here
 private object DeleteObject = null;

将删除按钮替换为此

protected void lnkDelete_Click(object sender, EventArgs e)
{
   this.DeleteObject = sender;
   Auth.style.remove("visibility");       
}