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
430 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
Comment Utility
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
Comment Utility
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
Comment Utility
Where in the second example, the "UserName" should be "UserGroup".
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
Are you using Telerik?
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
//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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry remove (("UserName")) and replace it with (!ServiceSupportHasAccessRole))
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:Friman001
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you disable the Column, does that disable all the CheckBoxes too?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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!
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

14 Experts available now in Live!

Get 1:1 Help Now