Solved

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

Posted on 2011-09-09
16
441 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:newjeep19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 2
16 Comments
 
LVL 4

Expert Comment

by:Friman001
ID: 36512430
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

0
 

Author Comment

by:newjeep19
ID: 36512442
The permission level that i am using is this:
  if (!ServiceSupportHasAccessRole)
            {
                //LOCK CHECKBOX COLUMN
               this.chkSNTOrder.Enabled = false;
                //chkSNTOrder.Visible = false;
            }
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512444
Where in the second example, the "UserName" should be "UserGroup".
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Friman001
ID: 36512447
Are you using Telerik?
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512461
//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

0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 36512464
Hello, try:
if (!ServiceSupportHasAccessRole)
{
    // 3 is the index of the column that you want to hide.
    MyGridView.Columns[3].Visible = false;
}

Open in new window

0
 

Author Comment

by:newjeep19
ID: 36512465
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?
0
 

Author Comment

by:newjeep19
ID: 36512473
sorry remove (("UserName")) and replace it with (!ServiceSupportHasAccessRole))
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512488
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.
0
 

Author Closing Comment

by:newjeep19
ID: 36512522
That works great......trying to get use to gridviews and thinking along those lines. Thank you for your help
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512529
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!
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512536
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

0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512540
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!!!
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36512556
If you disable the Column, does that disable all the CheckBoxes too?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36512572
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

0
 

Author Comment

by:newjeep19
ID: 36525539
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.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question