Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
XML
<asp:TemplateField>
                 <ItemTemplate>
                     <asp:LinkButton ID="Delete_LinkButton" runat="server" onclick="LinkButton1_Click1" CommandArgument='<%# Eval("UserName","{0}") %>'>LinkButton</asp:LinkButton>
                 </ItemTemplate>
             </asp:TemplateField>

C#
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.
Posted
Updated 25-Mar-11 0:17am
v2

1. You don't need to use Username like unique identifier. Because if 2 users have the same names they will be deleted both when clicking on your link button.

2. Your code is an implementation of SmartUI antipattern. Why don't you want to separate your application into layers?

3. Look at this article: http://forums.asp.net/t/1168036.aspx/1?Gridview+passing+the+DataKey+with+CommandArgument
 
Share this answer
 
Comments
m@dhu 25-Mar-11 6:28am    
Good points.
<asp:LinkButton ID="Delete_LinkButton" runat="server" onclick="LinkButton1_Click1" CommandName="delete" CommandArgument='<%# Eval("UserName","{0}") %>'>LinkButton</asp:LinkButton>


C#
protected void GridView1_RowCommand(object sender,
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "delete")
  {
    //your code to delete

  }
}


Also check this cp article
GridView Delete, with Confirmation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900