如何在gridview中使用LinkBut​​ton删除所选的用户名? [英] How to use a LinkButton inside gridview to delete selected username?

查看:74
本文介绍了如何在gridview中使用LinkBut​​ton删除所选的用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的"JobPost.mdf"中有一个"UserDetail"表.
我有一个"Gridview1",显示"UserDetail"表中的列,该表具有主键"UserName".
此"UserName"最初是使用Membership类功能保存的.
现在,我将一个删除"链接按钮添加到GridView1.此删除"不是自动生成按钮,我从工具箱中拖动到列itemtemplate内.
GridView1的列现在变为"Delete_LinkBut​​ton" +用户名"(在UserDetail表内)+城市"(在UserDetail表内)+"IsAdmin"(在UserDetail表内)

我需要的是,通过单击此"delete_linkBut​​ton",它将仅从"UserDetail"表中删除同一行上的整个用户实体(通过相应的"UserName"链接),以及从AspNetDB中删除所有信息. mdf(用户,会员资格,UserInRole等).

我想激发用户确认,但不是强制性的.至少我正在尝试使其以正确的方式起作用.

例如:

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



当我单击第一行上的删除"时,我需要删除"UserDetail"表中有关"ken"的所有记录.同时,AspNetDB.mdf中有关"ken"的所有记录都将消失,包括UserinRole表.

我是asp.net的新手,所以我不知道如何将"Delete_LinkBut​​ton"的命令参数传递给代码隐藏文件LinkBut​​ton1_Click(object sender,EventArgs e),因为我需要一个额外的参数"UserName".

我的部分代码如下:



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.

推荐答案

1.您不需要像唯一标识符那样使用用户名.因为如果2个用户具有相同的名称,则单击链接按钮时,两个用户都将被删除.

2.您的代码是SmartUI反模式的实现.为什么不要将应用程序分成多个层?

3.看这篇文章: http://forums.asp .net/t/1168036.aspx/1?Gridview + passing + the + DataKey + with + CommandArgument
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


<asp:LinkButton ID="Delete_LinkButton" runat="server" onclick="LinkButton1_Click1" CommandName="delete" CommandArgument=''<%# Eval("UserName","{0}") %>''>LinkButton</asp:LinkButton>





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

  }
}



还要检查此cp文章
带有确认的GridView删除 [



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


这篇关于如何在gridview中使用LinkBut​​ton删除所选的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆