0

I have a Asp Gridview Control with OnRowCommand .i have asp:ButtonField for edit / delete the values.

When I click the any one of the button

protected void gvStaffList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteStaff")
    {
    }
}

is not firing can you please help where i am in wrong. Below I included the entire code stuff 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

}

Javascript code

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

thanks in Advance

Karthik KM
  • 33
  • 2
  • 9
  • is the client-side onclick event working? – wazz May 20 '17 at 06:10
  • In client side console am getting this issue Uncaught ReferenceError: ShowLoadingPanel is not defined at Array.beginRequest (StaffList.aspx:28) at ScriptResource.axd:3484 at Sys$WebForms$PageRequestManager$_onFormSubmit [as _onFormSubmit] (ScriptResource.axd:1284) at Sys$WebForms$PageRequestManager$_doPostBack [as _doPostBack] (ScriptResource.axd:824) at ScriptResource.axd:47 at :1:1 – Karthik KM May 20 '17 at 06:24
  • Thanks for you Quick Reply wazz – Karthik KM May 20 '17 at 06:27
  • there's something else going wrong with asp.net ajax. i doubt we can figure it out from here. me at least. sry. – wazz May 20 '17 at 06:28
  • wazz,I added javascript code as well on top can you please look into that. – Karthik KM May 20 '17 at 06:40
  • you've added `function beginRequest(sender, args) { ShowLoadingPanel(); }`. when the request begins (postback, etc) it looks for the function `ShowLoadingPanel()`, but it does not exist. – wazz May 20 '17 at 06:45

0 Answers0