How to use a LinkButton inside gridview to delete selected username in the code-behind file?

jenifer picture jenifer · Mar 25, 2011 · Viewed 13.9k times · Source

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!

Answer

Raphael picture Raphael · Aug 26, 2011

For the confirm, you should add the script to the OnClientClick property of the LinkButton:

<asp:LinkButton 
    ID="Delete_LinkButton" 
    runat="server" 
    onclick="LinkButton1_Click1" 
    CommandArgument='<%# Eval("UserName","{0}") %>'
    OnClientClick='if (!confirm("Are you sure you want to delete <%# Eval("UserName","{0}") %>?")) return false;'
>
    LinkButton
</asp:LinkButton>

And your event handler for that button should be:

protected void Delete_LinkButton_Click(object sender, EventArgs e)
{
    string userName = ((LinkButton)sender).CommandArgument.ToString();

    Membership.DeleteUser(UserName);
    JobPostDataContext db = new JobPostDataContext();

     foreach (var item in db.UserDetails.Where(u => u.UserName == userName))
          db.UserDetails.DeleteOnSubmit(Item);

    db.SubmitChanges();
}

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, the LinkButton. So, you can cast it to LinkButton and get its CommandArgument property, where tye UserName is.