C# 在asp.net c中单击“编辑/删除”时未触发GridView行命令#

C# 在asp.net c中单击“编辑/删除”时未触发GridView行命令#,c#,asp.net,gridview,edit,rowcommand,C#,Asp.net,Gridview,Edit,Rowcommand,我有一个带有onrow命令的Asp Gridview控件。我有用于编辑/删除值的Asp:ButtonField 当我单击任意一个按钮时 protected void gvStaffList_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DeleteStaff") { } } 我没有开枪,你能帮我一下吗。 下面我包括了所有的代码 StaffList.aspx &l

我有一个带有onrow命令的Asp Gridview控件。我有用于编辑/删除值的Asp:ButtonField

当我单击任意一个按钮时

protected void gvStaffList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteStaff")
    {
    }
}
我没有开枪,你能帮我一下吗。 下面我包括了所有的代码 StaffList.aspx

<asp:GridView ID="gvStaffList" runat="server" 
    AllowPaging="false" 
    AllowSorting="True" 
    OnPageIndexChanging="gvStaffList_PageIndexChanging"
    GridLines="Horizontal" 
    AutoGenerateColumns="False" 
    Width="100%" 
    OnRowCommand="gvStaffList_RowCommand"
    DataKeyNames="AliasID" 
    OnRowDataBound="gvStaffList_RowDataBound"
    OnSorting="gvStaffList_Sorting"
    CssClass="gvHeader">
    <Columns>
        <asp:BoundField 
            DataField="AliasID" 
            HeaderText="S.No" SortExpression="AliasId" HeaderStyle-
            HorizontalAlign="Left"
            ItemStyle-HorizontalAlign="Left" />
        <asp:BoundField 
            DataField="Name" 
            ItemStyle-
            Wrap="true" 
            HeaderText="Name" 
            SortExpression="Name"
            HeaderStyle-HorizontalAlign="Left" 
            ItemStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="Description" 
            ItemStyle-Wrap="true" 
            HeaderText="Title"
            SortExpression="Description" 
            HeaderStyle-HorizontalAlign="Left" 
            ItemStyle-HorizontalAlign="Left" />
        <asp:ButtonField 
            CommandName="EditStaff" 
            Text="Edit" 
            ItemStyle-HorizontalAlign="Left" />
        <asp:ButtonField 
            CommandName="DeleteStaff" 
            Text="Delete" 
            ItemStyle-HorizontalAlign="Left" />
    </Columns>
    <HeaderStyle CssClass="gvHeader" />
    <RowStyle CssClass="gvRow" />
    <AlternatingRowStyle CssClass="gvAltRow" />
</asp:GridView>

StaffList.aspx.cs

using System;
using System.Linq;
using System.Web.UI.WebControls;

public partial class CMS_StaffList : Page_Base_Admin
{
#region Properties, Constants, Enums etc.
public const string vwStatSort = "sortOrder";
public const string ascOrder = "asc";
public const string descOrder = "desc";
public const string titleAddmbr = "Add Staff Member";
public string sortOrder
{
    get
    {
        if (ViewState[vwStatSort].ToString() == descOrder || ViewState[vwStatSort] == null)
            ViewState[vwStatSort] = ascOrder;
        else
            ViewState[vwStatSort] = descOrder;

        return ViewState[vwStatSort].ToString();
    }
    set
    {
        ViewState[vwStatSort] = value;
    }
}
#endregion

#region Page related Events
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ViewState[vwStatSort] = descOrder;
        Fill_gvStaffList(StaffListSort.AliasId.ToString());
        this.divAddNewStaffMember.Visible = false;
        this.divNewStaffMember.Visible = true;
    }
}
#endregion

#region Events of Grid view 'gvStaffList'
protected void gvStaffList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    //Fill_gvStaffList(StaffListSort.AliasId.ToString());
}
protected void gvStaffList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteStaff")
    {
        try
        {
            NotificationAlias ntfnAlias = new NotificationAlias
            {
                AliasId = Convert.ToInt32(gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0]),
                Description = "",
                Name = ""
            };
            int result = Core.SaveOrUpdateNotificationAlias(ntfnAlias, true);
            ViewState[vwStatSort] = descOrder;
            Fill_gvStaffList(StaffListSort.AliasId.ToString());
        }
        catch (Exception)
        {

        }
    }
    else if (e.CommandName == "EditStaff")
    {
        try
        {
            txtStaffName.Text = gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString();
            txtStaffTitle.Text = gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[2].ToString();
        }
        catch (Exception)
        {

        }
    }
}

/// <summary>
/// Handles the RowDataBound event of the gvStaffList control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
protected void gvStaffList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        NotificationAlias drv = (NotificationAlias)e.Row.DataItem;
        if (drv.AliasIsActive == false)
        {
            e.Row.Enabled = false;
            row.CssClass = "disabled";
            e.Row.Attributes["style"] = "none";
        }
        else
        {
            //For deleting
            LinkButton button = (LinkButton)e.Row.Cells[4].Controls[0];
            button.Attributes.Add("onclick", "return confirm('Do you want to deactivate this user. This action cannot be undone. This will not affect past course updates sent by user.');");
        }
    }
}


/// <summary>
/// Handles the Sorting event of the gvStaffList control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewSortEventArgs"/> instance containing the event data.</param>
protected void gvStaffList_Sorting(object sender, GridViewSortEventArgs e)
{
    string directn = e.SortDirection.ToString();
    if (this.Sort == e.SortExpression)
    {
        this.Sort = this.Sort;

    }
    else
    {
        this.Sort = e.SortExpression;
    }
    Fill_gvStaffList(this.Sort);
}

/// <summary>
/// Fill grid view gvstafflist.
/// </summary>
protected void Fill_gvStaffList(string sortfield)
{
    StaffListSort gvstaffSort = (StaffListSort)Enum.Parse(typeof(StaffListSort), sortfield);
    switch (gvstaffSort)
    {
        case StaffListSort.AliasId:
            if (sortOrder == ascOrder)
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderBy(x => x.AliasId).ToList();
            else
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderByDescending(x => x.AliasId);
            break;
        case StaffListSort.Description:
            if (sortOrder == ascOrder)
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderBy(x => x.Description).ToList();
            else
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderByDescending(x => x.Description).ToList();
            break;
        case StaffListSort.Name:
            if (sortOrder == ascOrder)
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderBy(x => x.Name).ToList();
            else
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderByDescending(x => x.Name).ToList();
            break;
        default:
            if (sortOrder == ascOrder)
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderBy(x => x.AliasId).ToList();
            else
                gvStaffList.DataSource = Core.GetAllNotificationAlias().OrderByDescending(x => x.AliasId).ToList();
            break;
    }
    gvStaffList.DataBind();
}
#endregion

#region Button Click events
/// <summary>
/// Handles the Click event of the btnNewStaffMember control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnNewStaffMember_Click(object sender, EventArgs e)
{
    this.divAddNewStaffMember.Visible = true;
    this.divNewStaffMember.Visible = false;
    lblAddStaff.Text = titleAddmbr;
    txtStaffName.Focus();
}

/// <summary>
/// Handles the Click event of the btnSave control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnSave_Click(object sender, EventArgs e)
{
    int saveAlias;
    var alias = new NotificationAlias();
    alias.Name = txtStaffName.Text.Trim();
    alias.Description = txtStaffTitle.Text.Trim();
    saveAlias = Core.SaveOrUpdateNotificationAlias(alias, false);
    if (saveAlias >= 0)
    {
        ViewState[vwStatSort] = descOrder;
        Fill_gvStaffList(StaffListSort.AliasId.ToString());
        txtStaffName.Text = "";
        txtStaffTitle.Text = "";
        txtStaffName.Focus();
    }
}

/// <summary>
/// Handles the Click event of the btnCancel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnCancel_Click(object sender, EventArgs e)
{
    this.txtStaffName.Text = "";
    this.txtStaffTitle.Text = "";
    this.divAddNewStaffMember.Visible = false;
    this.divNewStaffMember.Visible = true;
}
#endregion
使用系统;
使用System.Linq;
使用System.Web.UI.WebControl;
公共部分类CMS\u StaffList:Page\u Base\u Admin
{
#区域属性、常量、枚举等。
公共常量字符串vwStatSort=“sortOrder”;
公共常量字符串ascOrder=“asc”;
公共常量字符串descOrder=“desc”;
public const string titleAddmbr=“添加工作人员”;
公共字符串排序器
{
得到
{
if(ViewState[vwStatSort].ToString()==descOrder | | ViewState[vwStatSort]==null)
ViewState[vwStatSort]=ascOrder;
其他的
ViewState[vwStatSort]=descOrder;
返回ViewState[vwStatSort].ToString();
}
设置
{
ViewState[vwStatSort]=值;
}
}
#端区
#区域页面相关事件
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
ViewState[vwStatSort]=descOrder;
Fill_gvStaffList(StaffListSort.AliasId.ToString());
this.divAddNewStaffMember.Visible=false;
this.divNewStaffMember.Visible=true;
}
}
#端区
#网格视图“gvStaffList”的区域事件
受保护的void gvStaffList_PageIndexChanging(对象发送方,GridViewPageEventArgs e)
{
//Fill_gvStaffList(StaffListSort.AliasId.ToString());
}
受保护的void gvStaffList_row命令(对象发送方,GridViewCommandEventArgs e)
{
如果(e.CommandName==“DeleteStaff”)
{
尝试
{
NotificationAlias ntfnAlias=新NotificationAlias
{
别名ID=Convert.ToInt32(gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)]。值[0]),
Description=“”,
Name=“”
};
int result=Core.SaveOrUpdateNotificationAlias(ntfnAlias,true);
ViewState[vwStatSort]=descOrder;
Fill_gvStaffList(StaffListSort.AliasId.ToString());
}
捕获(例外)
{
}
}
else if(e.CommandName==“EditStaff”)
{
尝试
{
txtStaffName.Text=gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString();
txtStaffTitle.Text=gvStaffList.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[2].ToString();
}
捕获(例外)
{
}
}
}
/// 
///处理gvStaffList控件的RowDataBound事件。
/// 
///事件的来源。
///包含事件数据的实例。
受保护的void gvStaffList_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
GridViewRow行=e.行;
如果(e.Row.RowType==DataControlRowType.DataRow)
{
NotificationAlias drv=(NotificationAlias)e.Row.DataItem;
if(drv.AliasIsActive==false)
{
e、 Row.Enabled=false;
row.CssClass=“已禁用”;
e、 行属性[“样式”]=“无”;
}
其他的
{
//删除
LinkButton=(LinkButton)e.Row.Cells[4]。控件[0];
添加(“onclick”,“return confirm('是否要停用此用户。此操作无法撤消。这不会影响用户发送的过去课程更新');”;
}
}
}
/// 
///处理gvStaffList控件的排序事件。
/// 
///事件的来源。
///包含事件数据的实例。
受保护的无效gvStaffList_排序(对象发送器、GridViewSortEventArgs e)
{
string directn=e.SortDirection.ToString();
if(this.Sort==e.SortExpression)
{
this.Sort=this.Sort;
}
其他的
{
this.Sort=e.SortExpression;
}
填写表格(this.Sort);
}
/// 
///填充网格视图gvstafflist。
/// 
受保护的空白填充列表(字符串排序字段)
{
StaffListSort gvstaffSort=(StaffListSort)Enum.Parse(typeof(StaffListSort),sortfield);
开关(gvstaffSort)
{
case StaffListSort.AliasId:
if(sortOrder==ascOrder)
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderBy(x=>x.AliasId.ToList();
其他的
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderByDescending(x=>x.AliasId);
打破
案例StaffListSort。描述:
if(sortOrder==ascOrder)
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderBy(x=>x.Description.ToList();
其他的
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderByDescending(x=>x.Description).ToList();
打破
案例StaffListSort。名称:
if(sortOrder==ascOrder)
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderBy(x=>x.Name.ToList();
其他的
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderByDescending(x=>x.Name).ToList();
打破
违约:
if(sortOrder==ascOrder)
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderBy(x=>x.AliasId.ToList();
其他的
gvStaffList.DataSource=Core.GetAllNotificationAlias().OrderByDescending(x=>x.AliasId.ToList();
打破
}
gvStaffList.DataBind();
}
#端区
#区域按钮单击事件
/// 
///处理btnNewStaffMember控件的单击事件。
/// 
///事件的来源。
///包含事件数据的实例。
受保护的无效btnNewStaffMember_单击(对象发送方,事件参数e)
{
this.divAddNewStaffMember.Visible=true;
this.divNewStaffMember.Visible=false;
lblAddStaff.Text=titleAddmbr;
txtStaff
    <script type="text/javascript" language="javascript">
    function pageLoad(sender, args) {
        //  register for our events
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
    }

    function beginRequest(sender, args) {
        ShowLoadingPanel();
    }

    function endRequest(sender, args) {
        HideLoadingPanel();
    }
    function Validate() {
        var name = trim(document.getElementById("<%=txtStaffName.ClientID%>").value.toString());
        if (name == "") {
            alert("Please enter staff name.");
            document.getElementById("<%=txtStaffName.ClientID%>").value = "";
            document.getElementById("<%=txtStaffName.ClientID%>").focus();
            return false;
        }

        var title = trim(document.getElementById("<%=txtStaffTitle.ClientID%>").value.toString());
        if (title == "") {
            alert("Please enter title.");
            document.getElementById("<%=txtStaffTitle.ClientID%>").value = "";
            document.getElementById("<%=txtStaffTitle.ClientID%>").focus();
            return false;
        }
        return true;
    }
    function trim(stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    }

    function ValidateLength(label) {
        if (label.toString() == 'name') {
            if ((document.getElementById("<%=txtStaffName.ClientID%>").value).length == 500)
                document.getElementById("<%=dvNameLabel.ClientID%>").style.display = 'block';
            else
                document.getElementById("<%=dvNameLabel.ClientID%>").style.display = 'none';
        }

        if (label.toString() == 'title') {
            if ((document.getElementById("<%=txtStaffTitle.ClientID%>").value).length == 500)
                document.getElementById("<%=dvTitleLabel.ClientID%>").style.display = 'block';
            else
                document.getElementById("<%=dvTitleLabel.ClientID%>").style.display = 'none';
        }
    }
</script>