如何使用 gridview 内的 LinkBut​​ton 删除代码隐藏文件中选定的用户名?

发布于 2024-10-25 19:53:19 字数 1903 浏览 1 评论 0原文

我的“JobPost.mdf”中有一个“UserDetail”表。 我有一个“Gridview1”显示“UserDetail”表中的列,该表具有主键“UserName”。 这个“UserName”最初是使用Membership类函数保存的。 现在我向 GridView1 添加一个“删除”链接按钮。这个“删除”不是自动生成按钮,我从工具箱拖到列项目模板内。 GridView1 的列现在变成“Delete_LinkBut​​ton”+“UserName”(在 UserDetail 表中)+“City”(在 UserDetail 表中)+“IsAdmin”(在 UserDetail 表中)

我需要的是通过单击此“delete_linkBut​​ton”,它只会从“UserDetail”表中删除同一行上的整个用户实体(通过相应的“UserName”链接),并删除 AspNetDB.mdf 中的所有信息(用户、成员身份、UserInRole 等)。

我想启动用户确认,但不是强制性的。至少我正在努力让它以正确的方式发挥作用。

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”。

我的部分代码如下:

<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();


     }

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2024-11-01 19:53:19

对于确认,您应该将脚本添加到 LinkBut​​ton 的 OnClientClick 属性中:

<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>

并且该按钮的事件处理程序应该是:

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();
}

OnClientClick 属性添加您写入的 JavaScript它到所呈现的 标记的 onclick 属性的开头。所以,如果用户没有确认删除,它将返回 false 并且不执行任何操作。否则,它将回发删除。

在事件处理程序中,发送者始终是发起事件的控件。在本例中,为LinkBut​​ton。因此,您可以将其转换为 LinkBut​​ton 并获取其 CommandArgument 属性,其中 UserName 所在位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文