0

I am using the following code to delete the records from database.

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);
}

The delete link button will delete the record. I would like to add a pop-up window when user click on the delete button and ask them to type in password, if password correct then delete happen otherwise they could not delete the record. How could i do this either using JavaScript or code behind?

Jared Lovin
  • 543
  • 9
  • 24
TaroYuki
  • 147
  • 3
  • 16
  • Are you using JQuery.UI on your client side code by any chance? You could create a custom dialog with a user name and password box. – Mike Jan 09 '17 at 20:19
  • @Mmcgowa3, No, i dont use the Jquery.UI – TaroYuki Jan 09 '17 at 20:21
  • Possible duplicate of [How to use a LinkButton inside gridview to delete selected username in the code-behind file?](http://stackoverflow.com/questions/5430984/how-to-use-a-linkbutton-inside-gridview-to-delete-selected-username-in-the-code) – Heretic Monkey Jan 09 '17 at 21:41

1 Answers1

0

Add an attribute in asp button OnClientClick

ASP Code

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

Script

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

Another Way To Authenticate

*.aspx (Design File)

add simple 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 File **

create an Event for btnAuth Button

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

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

}

Create a function that run your delete command

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);    
  }

Create a Global Variable Name DeleteObject

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

Replace Your Delete Button to this

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