How to use a LinkButton inside gridview to delete selected username in the code-behind file?
Posted
by
jenifer
on Stack Overflow
See other posts from Stack Overflow
or by jenifer
Published on 2011-03-25T10:04:46Z
Indexed on
2012/09/30
3:38 UTC
Read the original article
Hit count: 234
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!
Thanks in advance.
© Stack Overflow or respective owner