如何使用 gridview 内的 LinkButton 删除代码隐藏文件中选定的用户名?
我的“JobPost.mdf”中有一个“UserDetail”表。 我有一个“Gridview1”显示“UserDetail”表中的列,该表具有主键“UserName”。 这个“UserName”最初是使用Membership类函数保存的。 现在我向 GridView1 添加一个“删除”链接按钮。这个“删除”不是自动生成按钮,我从工具箱拖到列项目模板内。 GridView1 的列现在变成“Delete_LinkButton”+“UserName”(在 UserDetail 表中)+“City”(在 UserDetail 表中)+“IsAdmin”(在 UserDetail 表中)
我需要的是通过单击此“delete_linkButton”,它只会从“UserDetail”表中删除同一行上的整个用户实体(通过相应的“UserName”链接),并删除 AspNetDB.mdf 中的所有信息(用户、成员身份、UserInRole 等)。
我想启动用户确认,但不是强制性的。至少我正在努力让它以正确的方式发挥作用。
for example:
Command UserName City IsAdmin
delete ken Los Angles TRUE
delete jim Toronto FALSE
当我单击第一行上的“删除”时,我需要删除“UserDetail”表中有关“ken”的所有记录。同时,AspNetDB.mdf中所有有关“ken”的记录都将消失,包括UserinRole表。
我是asp.net新手,所以我不知道如何将“Delete_LinkButton”的命令参数传递给代码隐藏文件LinkButton1_Click(object sender, EventArgs e),因为我需要一个额外的参数“UserName”。
我的部分代码如下:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Delete_LinkButton" runat="server" onclick="LinkButton1_Click1" CommandArgument='<%# Eval("UserName","{0}") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void Delete_LinkButton_Click(object sender, EventArgs e)
{
((LinkButton) GridView1.FindControl("Delete_LinkButton")).Attributes.Add("onclick", "'return confirm('Are you sure you want to delete {0} '" + UserName);
Membership.DeleteUser(UserName);
JobPostDataContext db = new JobPostDataContext();
var query = from u in db.UserDetails
where u.UserName == UserName
select u;
for (var Item in query)
{
db.UserDetails.DeleteOnSubmit(Item);
}
db.SubmitChanges();
}
请帮忙!
I have a "UserDetail" table in my "JobPost.mdf".
I have a "Gridview1" showing the column from "UserDetail" table,which has a primary key "UserName".
This "UserName" is originally saved using Membership class function.
Now I add a "Delete" linkbutton to the GridView1. This "Delete" is not autogenerate button,I dragged inside the column itemtemplate from ToolBox.
The GridView1's columns now become "Delete_LinkButton"+"UserName"(within the UserDetail table)+"City"(within the UserDetail table)+"IsAdmin"(within the UserDetail table)
What I need is that by clicking this "delete_linkButton",it will ONLY delete the entire User Entity on the same row (link by the corresponding "UserName") from the "UserDetail" table,as well as delete all information from the AspNetDB.mdf (User,Membership,UserInRole,etc).
I would like to fireup a user confirm,but not mandatory. At least I am trying to make it functional in the correct way.
for example:
Command UserName City IsAdmin
delete ken Los Angles TRUE
delete jim Toronto FALSE
When I click "delete" on the first row, I need all the record about "ken" inside the "UserDetail" table to be removed. Meanwhile, all the record about "ken" in the AspNetDB.mdf will be gone, including UserinRole table.
I am new to asp.net, so I don't know how to pass the commandargument of the "Delete_LinkButton" to the code-behind file LinkButton1_Click(object sender, EventArgs e), because I need one extra parameter "UserName".
My partial code is listed below:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Delete_LinkButton" runat="server" onclick="LinkButton1_Click1" CommandArgument='<%# Eval("UserName","{0}") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void Delete_LinkButton_Click(object sender, EventArgs e)
{
((LinkButton) GridView1.FindControl("Delete_LinkButton")).Attributes.Add("onclick", "'return confirm('Are you sure you want to delete {0} '" + UserName);
Membership.DeleteUser(UserName);
JobPostDataContext db = new JobPostDataContext();
var query = from u in db.UserDetails
where u.UserName == UserName
select u;
for (var Item in query)
{
db.UserDetails.DeleteOnSubmit(Item);
}
db.SubmitChanges();
}
Please do help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于确认,您应该将脚本添加到 LinkButton 的
OnClientClick
属性中:并且该按钮的事件处理程序应该是:
OnClientClick
属性添加您写入的 JavaScript它到所呈现的标记的 onclick 属性的开头。所以,如果用户没有确认删除,它将返回 false 并且不执行任何操作。否则,它将回发删除。
在事件处理程序中,
发送者
始终是发起事件的控件。在本例中,为LinkButton
。因此,您可以将其转换为LinkButton
并获取其CommandArgument
属性,其中UserName
所在位置。For the confirm, you should add the script to the
OnClientClick
property of the LinkButton:And your event handler for that button should be:
The
OnClientClick
property adds the JavaScript you write to it to the beggining of the onclick property of the rendered<a/>
tag. So, if the user does not confirm the delete, it will return false and do nothing. Else, it will do the postback to delete.In the event handler, the
sender
is always the control who originated the event. In this case, theLinkButton
. So, you can cast it toLinkButton
and get itsCommandArgument
property, where tyeUserName
is.