mardi 19 avril 2016

Compilation error finding definition for a button - ASP.NET

I keep getting a really strange, and probably ridiculously obvious error, regarding a button with the error saying

Compiler Error Message: CS1061: 'admin_roles_aspx' does not contain a definition for 'DeleteRole' and no extension method 'DeleteRole' accepting a first argument of type 'admin_roles_aspx' could be found (are you missing a using directive or an assembly reference?)

First off I have 'Button1' with the "DeleteRole" as an OnCommand as follows

<asp:GridView runat="server" ID="UserRoles" AutoGenerateColumns="false"
    CssClass="list" AlternatingRowStyle-CssClass="odd" GridLines="none"
    >
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>Role Name</HeaderTemplate>
            <ItemTemplate>
                <%# Eval("Role Name") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>User Count</HeaderTemplate>
            <ItemTemplate>
                <%# Eval("User Count") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Delete Role</HeaderTemplate>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" OnCommand="DeleteRole" CommandName="DeleteRole" CommandArgument='<%# Eval("Role Name") %>' Text="Delete" OnClientClick="return confirm('Are you sure?')" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then I have 'DeleteRole' created in roles.aspx.cs

public partial class roles : System.Web.UI.Page
    {
        private bool createRoleSuccess = true;

        private void Page_PreRender()
        {
            // Create a DataTable and define its columns
            DataTable RoleList = new DataTable();
            RoleList.Columns.Add("Role Name");
            RoleList.Columns.Add("User Count");

            string[] allRoles = Roles.GetAllRoles();

            // Get the list of roles in the system and how many users belong to each role
            foreach (string roleName in allRoles)
            {
                int numberOfUsersInRole = Roles.GetUsersInRole(roleName).Length;
                string[] roleRow = { roleName, numberOfUsersInRole.ToString() };
                RoleList.Rows.Add(roleRow);
            }

            // Bind the DataTable to the GridView
            UserRoles.DataSource = RoleList;
            UserRoles.DataBind();

            if (createRoleSuccess)
            {
                // Clears form field after a role was successfully added. Alternative to redirect technique I often use.
                NewRole.Text = "";
            }
        }
        private void DeleteRole(object sender, CommandEventArgs e)
        {
            try
            {
                Roles.DeleteRole(e.CommandArgument.ToString());
                ConfirmationMessage.InnerText = "Role '" + e.CommandArgument.ToString() + "' was deleted.";
            }
            catch (Exception ex)
            {
                ConfirmationMessage.InnerText = ex.Message;
            }
        }

Does anyone have any suggestions as its starting to really frustrate me now (probably down to my programming inexperience). Thanks in advance and please be gentle if I've been an idiot as my skills are quite evidently not great. Thanks.




Aucun commentaire:

Enregistrer un commentaire