Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

How to enable disable a asp.net checkbox in the page load based off of a premission level

I need to enable annd disable a column of checkboxes when the grid is loaded based off of the users premission level. the code is below
 protected void Page_Load(object sender, EventArgs e)
        {
           
            //CHECK USER ACCESS
            UserID = HttpContext.Current.User.Identity.Name;
            util = new Utils(crmUrl, crmOrganization, crmUserName, crmPassword, crmDomain);
            ServiceSupportHasAccessRole = util.GetLoggedInUserAndCheckPermission(UserID, ServiceSupportAccessRole);

            if (!ServiceSupportHasAccessRole)
            {
                //LOCK CHECKBOX COLUMN
               this.chkSNTOrder.Enabled = false;
                //chkSNTOrder.Visible = false;
            }

            //Test for ID if Null than print out error
            if (string.IsNullOrEmpty(_orderId))
            {
                gvParent.Visible = false;
                lblMessage.Text = "Unable to retrieve Order Id";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Black;

                if (!IsPostBack)
                {
                    // RETRIEVE PRODUCTS & BIND DATA TO THE GRID
                    BindParentGrid(new Guid(_orderId));
                }
            }

        }

Open in new window

Avatar of Friman001
Friman001
Flag of United States of America image

To check premission levels, you can use:
if (Page.User.Identity.Name == "UserName")
{do this}
else
{do that}

Open in new window


If you are using System.Web.Security, you could try:
if (Roles.IsUserInRole("UserName"))
{do this}
else
{do that}

Open in new window

Avatar of newjeep19

ASKER

The permission level that i am using is this:
  if (!ServiceSupportHasAccessRole)
            {
                //LOCK CHECKBOX COLUMN
               this.chkSNTOrder.Enabled = false;
                //chkSNTOrder.Visible = false;
            }
Where in the second example, the "UserName" should be "UserGroup".
Are you using Telerik?
//CHECK USER ACCESS
            UserID = HttpContext.Current.User.Identity.Name;
            util = new Utils(crmUrl, crmOrganization, crmUserName, crmPassword, crmDomain);
           [b] Boolean [/b]ServiceSupportHasAccessRole = util.GetLoggedInUserAndCheckPermission(UserID, ServiceSupportAccessRole);

            if (!ServiceSupportHasAccessRole)
            {
                //LOCK CHECKBOX COLUMN
               this.chkSNTOrder.Enabled = false;
                //chkSNTOrder.Visible = false;
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK so,if canI do this:
 if (Roles.IsUserInRole("UserName"))
(!ServiceSupportHasAccessRole))  
            {
                //LOCK CHECKBOX COLUMN
               this.chkSNTOrder.Enabled = false;
                //chkSNTOrder.Visible = false;
            }
if so will the column in the gridview that has the checkboxes chkSNTOrder be locked down? so that they can not click one or any of those checkboxes?
sorry remove (("UserName")) and replace it with (!ServiceSupportHasAccessRole))
Oh, you are not having troubles with authenication?  You would need to create a for loop to disable each CheckBox or try disabling the column.
That works great......trying to get use to gridviews and thinking along those lines. Thank you for your help
ContentPlaceHolder mpContentPlaceHolder;
        CheckBox mpCheckBox;
        mpContentPlaceHolder =
          (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        string name = "";
            if (mpContentPlaceHolder != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    name = "CheckBox" + (i + 2);

                    mpCheckBox =
                        (CheckBox)mpContentPlaceHolder.FindControl(name);
                    if (mpRadTextBox != null)
                    {
                        mpCheckBox.Visable = false;
                    }
                }

Open in new window


If you aren't using a Master Page, you can just search for the CheckBox without looking at the Master Page ContentHolder!
Without a Master Page, try this!

string name = "";

for (int i = 0; i < 4; i++)
                {
                    name = "CheckBox" + (i + 2);

                    mpCheckBox =
                        (CheckBox)Page.FindControl(name);  // Page.FindControl, Document.FindControl (not sure)
                    if (mpCheckBox != null)
                    {
                        mpCheckBox.Visable = false;
                    }
                }

Open in new window

You would need to customize the for loop to be the numbers of the CheckBoxes and also customize the assignment of name within the for loop!!!
If you disable the Column, does that disable all the CheckBoxes too?
Hello again, also, if you don't really want to hide the column, you can disable the checkboxes, test the next example:
<%@ Page Language="C#" EnableEventValidation="false" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    bool ServiceSupportHasAccessRole = false;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Dummy data...
            DataTable dtt = new DataTable();
            dtt.Columns.Add("Text", typeof(string));
            for (int i = 0; i < 6; i++)
                dtt.Rows.Add("Hello " + i);
            dtt.AcceptChanges();

            // Bind the data to the GridView
            GridView1.DataSource = dtt;
            GridView1.DataBind();

            // Example to get the permisions... default false...
            //ServiceSupportHasAccessRole = util.GetLoggedInUserAndCheckPermission(UserID, ServiceSupportAccessRole);
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (!ServiceSupportHasAccessRole)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = e.Row.FindControl("CheckBoxChecked") as CheckBox;
                if (cb != null)
                    cb.Enabled = false;
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            onrowcreated="GridView1_RowCreated">
            <Columns>
                <asp:BoundField DataField="Text" HeaderText="Text" />
                <asp:BoundField DataField="Text" HeaderText="Text 2" />
                <asp:BoundField DataField="Text" HeaderText="Text 3" />
                <asp:TemplateField HeaderText="Check">
                    <ItemTemplate>
                        <asp:CheckBox runat="server" ID="CheckBoxChecked" Text="" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Do Nothing..." />
    </div>
    </form>
</body>
</html>

Open in new window

Do you know about CRM 4.0 ?  I need to be to have a checkbox remain checked on a asp.Net grid view so that when i click the submit button the checked value updates or inserts into the CRM form. Then when I re load the grid the checked box is still cheked. If I need to submit a new question please let me know.