我正在尝试在gridview中将asp:Button与我的OnRowDeleting事件连接起来,但我不知道如何做到这一点:X。我目前正在使用CommandField,但出于我自己的目的,我需要使用Button。有人可以帮帮我吗?
编辑:
让我重新解释一下,我在同一个CommandField中有一行删除和编辑按钮。我想要做的是在某些特定情况下仅为特定行隐藏“删除”按钮,而不是每行都隐藏。这就是为什么我正在努力使用CommandField,因为没有ID随之而来,所以我不能在我的代码隐藏中引用它。但是 - asp:Button有一个ID但我无法将其与UserAccounts_RowDeleting函数链接起来。这是我的问题 - 如何链接它? :)
EDIT2:
这些是我的代码的一部分:
<asp:GridView ID="UserAccounts" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" AutoGenerateDeleteButton="false"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AutoGenerateEditButton="false"
onrowcancelingedit="UserAccounts_RowCancelingEdit" RowStyle-ForeColor="#3A3A3A" OnRowEditing="UserAccounts_RowEditing"
PageSize="10" AllowPaging="false" onrowdeleting="UserAccounts_RowDeleting"
OnRowUpdating="UserAccounts_RowUpdating" OnRowDataBound="UserAccounts_RowDataBound" onrowcreated="UserAccounts_RowCreated">
<Columns>
<asp:CommandField ShowDeleteButton="true" ShowEditButton='true' ButtonType="Link" />
<asp:BoundField DataField="UserName" HeaderText="Username" ReadOnly="true"/>
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:CheckBoxField DataField="IsApproved" HeaderText="Approved?" ReadOnly="false"/>
<asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked Out?" ReadOnly="true"/>
<asp:CheckBoxField DataField="IsOnline" HeaderText="Online?" ReadOnly="true"/>
<asp:BoundField DataField="Comment" HeaderText="Comment" NullDisplayText="N/A"/>
<asp:TemplateField HeaderText="Select" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
//Codebehind c#
protected void UserAccounts_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Funcs.GetAdminLevel() >= 999)
{
username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
if (username.ToLower().Equals(HttpContext.Current.User.Identity.Name.ToLower()))
ActionStatus.Text = string.Format("ERROR: You can't delete your own account ('<font color=#000000><b>{0}</b></font>')!", username);
else if (Funcs.GetAdminLevel(username) >= 1)
ActionStatus.Text = string.Format("ERROR: You can't delete administrators' accounts ('<font color=#000000><b>{0}</b></font>')!", username);
else
{
Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
Membership.DeleteUser(username);
ActionStatus.Text = string.Format("User '<font color=#000000><b>{0}</b></font>' has been successfully deleted!", username);
BindUserAccounts();
}
}
else
ActionStatus.Text = "You are not authorized to delete user accounts!";
}
我想将CommandField更改为ItemTemplate,这样我就可以通过特定行的代码隐藏来启用/禁用它。另外,我想要ItemTemplate,它是asp:Button,使用UserAccounts_RowDeleting事件,这就是我的问题所在,我只是不知道如何将按钮链接到事件..
答案 0 :(得分:1)
听起来你已经有了很多解决方案。使用TemplateField
作为按钮。关键部分是将按钮的CommandName
设置为&#34;删除&#34;。这告诉GridView它是一个删除按钮,RowDeleting
事件应该处理它。
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>