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

I have a table called "UserDetail" in my "JobPost.mdf". I have a "Gridview1" showing a column from the "UserDetail" table that has the primary key "UserName". This "UserName" is initially stored using the function of the Membership class. Now I am adding a Delete link to GridView1. This โ€œDeleteโ€ is not a self-generating button, and I dragged it into the itemtemplate element of the column from the ToolBox. Graphs GridView1 now become "Delete_LinkButton" + "UserName" (in the UserDetail table) + "City" (in the UserDetail table) + "IsAdmin" (in the UserDetail table)

I need to click this "delete_linkButton", it ONLY removes the entire user entity in the same row (the link corresponding to "UserName") from the "UserDetail" table, and also removes all information from AspNetDB.mdf (User, Membership, UserInRole, etc.) .d.).

I would like the user to be able to confirm, but not necessarily. At least I'm trying to make it functional correctly.

for example: Command UserName City IsAdmin delete ken Los Angles TRUE delete jim Toronto FALSE 

When I press delete in the first row, I need the entire ken entry inside the UserDetail table to be deleted. Meanwhile, all entries about "ken" in AspNetDB.mdf will disappear, including the UserinRole table.

I am new to asp.net, so I donโ€™t know how to pass the "Delete_LinkButton" command parameter to the LinkButton1_Click code file (object sender, EventArgs e), because I need one additional parameter "UserName" ".

My partial code is 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, help!

+4
source share
1 answer

To confirm, you must add a script to the OnClientClick property in 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 this 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 that you write to it to the beginning of the onclick property of the processed <a/> tag. Thus, if the user does not confirm the deletion, he will return false and will do nothing. Otherwise, a postback will be performed to delete.

In an event handler, sender always the control that triggered the event. In this case, LinkButton . That way, you can pass it to LinkButton and get its CommandArgument property, where tye is UserName .

+1
source

Source: https://habr.com/ru/post/1345209/


All Articles