protected void DgPortalRoleEditCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 {
     ErrorDisplay1.ClearError();
     try
     {
         dgPortalRole.SelectedIndex = e.Item.ItemIndex;
         int id = (DataCheck.IsNumeric(dgPortalRole.DataKeys[e.Item.ItemIndex].ToString())) ? int.Parse(dgPortalRole.DataKeys[e.Item.ItemIndex].ToString()) : 0;
         if (id < 1)
         {
             ErrorDisplay1.ShowError("Invalid Item Selection");
             return;
         }
         ErrorDisplay1.ClearControls(tbUserInfo);
         txtRoleName.Text       = ((LinkButton)dgPortalRole.SelectedItem.FindControl("lblRole")).Text;
         ViewState["mRoleName"] = ((LinkButton)dgPortalRole.SelectedItem.FindControl("lblRole")).Text;
         ViewState["mRecordID"] = id;
         HideTables();
         btnSubmit.CommandArgument = "2"; //Update
         btnSubmit.Text            = "Update Role";
         mpeDisplayJobDetails.Show();
     }
     catch (Exception ex)
     {
         ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
         ErrorDisplay1.ShowError(ex.Message);
     }
 }
        protected void DgPortalRoleDeleteCommand(Object source, DataGridCommandEventArgs e)
        {
            ErrorDisplay1.ClearError();
            try
            {
                dgPortalRole.SelectedIndex = e.Item.ItemIndex;
                int    id       = (DataCheck.IsNumeric(dgPortalRole.DataKeys[e.Item.ItemIndex].ToString())) ? int.Parse(dgPortalRole.DataKeys[e.Item.ItemIndex].ToString()) : 0;
                string roleName = ((LinkButton)dgPortalRole.SelectedItem.FindControl("lblRole")).Text;
                if (roleName.Length == 0)
                {
                    ErrorDisplay1.ShowError("Process Error! Please try again later");
                    return;
                }

                //Check if the role name is PortalAdmin, then terminate the process
                if (roleName.ToLower() == "PortalAdmin".ToLower())
                {
                    ErrorDisplay1.ShowError("Sorry, you can not delete this role");
                    return;
                }
                //Check to see that the current user has right to delete role
                if (!Page.User.IsInRole("PortalAdmin"))
                {
                    ErrorDisplay1.ShowError("Sorry, you are not authorized to delete a role");
                }

                //If role has registered users, remove the users from the role.
                try
                {
                    if (Roles.GetUsersInRole(roleName).Length > 0)
                    {
                        string[] mUsersInRole = Roles.GetUsersInRole(roleName);
                        Roles.RemoveUsersFromRole(mUsersInRole, roleName);
                    }
                }
                catch (Exception ex)
                {
                    ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                    ErrorDisplay1.ShowError(ex.Message);
                    return;
                }
                //'Now delete the role!
                if (Roles.DeleteRole(roleName, false))
                {
                    BindRoleItems();
                    ErrorDisplay1.ShowSuccess("Role Item Was Removed");
                }
                else
                {
                    ErrorDisplay1.ShowError("Role Item could not be removed");
                }
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ErrorDisplay1.ShowError(ex.Message);
                return;
            }
        }
        protected void DgPortalSiteMapDeleteCommand(Object source, DataGridCommandEventArgs e)
        {
            ErrorDisplay1.ClearError();
            try
            {
                dgPortalSiteMap.SelectedIndex = e.Item.ItemIndex;
                int id = (DataCheck.IsNumeric(dgPortalSiteMap.DataKeys[e.Item.ItemIndex].ToString())) ? int.Parse(dgPortalSiteMap.DataKeys[e.Item.ItemIndex].ToString()) : 0;
                if (id == 1)
                {
                    ErrorDisplay1.ShowError("The Root Tab Item Cannot Be Deleted");
                    return;
                }

                RemoveItem(id);
                Reset();
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
            }
        }
        protected void DgPortalSiteMapEditCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            ErrorDisplay1.ClearError();
            try
            {
                dgPortalSiteMap.SelectedIndex = e.Item.ItemIndex;
                int id = (DataCheck.IsNumeric(dgPortalSiteMap.DataKeys[e.Item.ItemIndex].ToString())) ? int.Parse(dgPortalSiteMap.DataKeys[e.Item.ItemIndex].ToString()) : 0;
                if (id == 1)
                {
                    ErrorDisplay1.ShowError("The Root Tab Item Cannot be edited");
                    return;
                }

                ErrorDisplay1.ClearControls(tbUserInfo);
                txtLink.Text           = ((Label)dgPortalSiteMap.SelectedItem.FindControl("lblLink")).Text;
                txtDescription.Text    = ((Label)dgPortalSiteMap.SelectedItem.FindControl("lblDescription")).Text;
                txtTabName.Text        = ((LinkButton)dgPortalSiteMap.SelectedItem.FindControl("lblTitle")).Text;
                ViewState["mRecordID"] = id;
                BindSiteMapItems();
                BindRoleList();

                string mRoles = ((Label)dgPortalSiteMap.SelectedItem.FindControl("lblRoles")).Text;
                CheckRoles(mRoles);

                int k = (DataCheck.IsNumeric(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblParent")).Text)) ? int.Parse(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblParent")).Text) : 0;
                if (k > 0)
                {
                    ddlTabParent.SelectedValue = k.ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    ddlTabParent.SelectedIndex = 0;
                }

                int wx = (DataCheck.IsNumeric(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblTabType")).Text)) ? int.Parse(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblTabType")).Text) : 0;
                if (wx > 0)
                {
                    ddlTabType.SelectedValue = wx.ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    ddlTabType.SelectedIndex = 0;
                }

                int px = (DataCheck.IsNumeric(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblTabOrder")).Text)) ? int.Parse(((Label)dgPortalSiteMap.SelectedItem.FindControl("lblTabOrder")).Text) : 0;
                if (px > 0)
                {
                    BindOrders();
                    if (ddlTabOrder.Items.Count > 0)
                    {
                        ddlTabOrder.SelectedValue = px.ToString(CultureInfo.InvariantCulture);
                    }
                }

                //HideMainDvs();
                //this.tbUserInfo.Visible = true;
                //this.detailDiv.Visible = true;

                btnSubmit.CommandArgument = "2"; // Update Item
                btnSubmit.Text            = "Update";
                mpeDisplayJobDetails.Show();
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
            }
        }
        protected void BtnSubmitClick(Object sender, EventArgs e)
        {
            ErrorDisplay1.ClearError();
            try
            {
                if (ddlTabParent.SelectedIndex == 0)
                {
                    ErrorDisplay1.ShowError("Please select parent tab");
                    return;
                }

                if (txtTabName.Text == "")
                {
                    ErrorDisplay1.ShowError("Tab name is required");
                    return;
                }

                if (ddlTabType.SelectedIndex == 0)
                {
                    ErrorDisplay1.ShowError("Please select tab type");
                    return;
                }


                if (ValidateRole() < 1)
                {
                    ErrorDisplay1.ShowError("You must select at least one role");
                    return;
                }


                string mRoles = String.Empty;
                for (int i = 0; i < chkRoles.Items.Count; i++)
                {
                    if (chkRoles.Items[i].Selected)
                    {
                        mRoles += ";" + chkRoles.Items[i].Value;
                    }
                }

                if (mRoles.Length > 0)
                {
                    if (mRoles.StartsWith(";"))
                    {
                        mRoles = mRoles.Substring(1);
                    }
                    if (mRoles.EndsWith(";"))
                    {
                        mRoles = mRoles.Substring(0, mRoles.Length - 1);
                    }
                }
                else
                {
                    ErrorDisplay1.ShowError("Tab name is required");
                    return;
                }


                var mInfo = new sitemap
                {
                    DateCreated = DateMap.GetLocalDate(),
                    Description = txtDescription.Text,
                    Parent      = (DataCheck.IsNumeric(ddlTabParent.SelectedValue.ToString(CultureInfo.InvariantCulture)))? int.Parse(ddlTabParent.SelectedValue.ToString(CultureInfo.InvariantCulture)): 0,
                    Roles       = mRoles,
                    Title       = txtTabName.Text,
                    TabType     = (DataCheck.IsNumeric(ddlTabType.SelectedValue.ToString(CultureInfo.InvariantCulture)))? int.Parse(ddlTabType.SelectedValue.ToString(CultureInfo.InvariantCulture)): 0,
                    TabOrder    = (DataCheck.IsNumeric(ddlTabOrder.SelectedValue.ToString(CultureInfo.InvariantCulture)))? int.Parse(ddlTabOrder.SelectedValue.ToString(CultureInfo.InvariantCulture)): 0,
                    Url         = txtLink.Text
                };


                switch (int.Parse(btnSubmit.CommandArgument))
                {
                case 1:     //Add
                    long k = (new PortalServiceManager()).AddSiteMap(mInfo);
                    if (k < 1)
                    {
                        if (k == -1)
                        {
                            ErrorDisplay2.ShowError("Duplicate Tab Name! This Tab Name is already registered");
                            mpeDisplayJobDetails.Show();
                            return;
                        }
                        ErrorDisplay2.ShowError("Item was not be added");
                        mpeDisplayJobDetails.Show();
                        return;
                    }
                    Reset();
                    //HideMainDvs();
                    BindSitemMapItemList();
                    //this.listDV.Visible = true;
                    ErrorDisplay1.ShowSuccess("Item Was Added Successfully");
                    mpeDisplayJobDetails.Hide();
                    break;

                case 2:     //Update
                    int id = (DataCheck.IsNumeric(ViewState["mRecordID"].ToString())) ? int.Parse(ViewState["mRecordID"].ToString()) : 0;
                    if (id < 1)
                    {
                        ErrorDisplay2.ShowError("Process Validation Failed!");
                        mpeDisplayJobDetails.Show();
                        return;
                    }
                    mInfo.ID = id;
                    int flag = 0;
                    if (!(new PortalServiceManager()).UpdateSiteMap(mInfo, ref flag))
                    {
                        if (flag == -1)
                        {
                            ErrorDisplay2.ShowError("Duplicate Tab Name! This Tab Name is already registered");
                            mpeDisplayJobDetails.Show();
                            return;
                        }
                        ErrorDisplay2.ShowError("Update failed!");
                        mpeDisplayJobDetails.Show();
                        return;
                    }
                    Reset();
                    BindSitemMapItemList();
                    btnSubmit.Text = "Add New Tab";
                    mpeDisplayJobDetails.Hide();
                    ErrorDisplay1.ShowSuccess("Item Was Updated Successfully");
                    break;
                }
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
            }
        }