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
431 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now