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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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