我使用以下代码从数据库中删除记录。
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 或代码隐藏来做到这一点?
在 asp 按钮中添加属性 在客户端单击上
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
为 btn身份验证按钮创建事件
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);
}
创建全局变量名称删除对象
//Global Linkbutton object set here
private object DeleteObject = null;
将您的删除按钮替换为此按钮
protected void lnkDelete_Click(object sender, EventArgs e)
{
this.DeleteObject = sender;
Auth.style.remove("visibility");
}