1
votes

I have an aspx form where I want to Enable and Disable some controls based on the rights assigned to the resprective Users.

The logic is something like this.

IF Condition matched to Y then enable the controls else The controls should be disabled.

So, I wrote the below code based on condition

if (Hid_Mode.Value != "M")
    {
        DataTable dtFill = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM WMS_MENU_RIGHTS WHERE MKEY = '" + Session["UserId"].ToString() + "' and MENU_MKEY = '" + Request.QueryString["menuid"].ToString() + "'", constr);
        sda.Fill(dtFill);

        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y")
        {
            //Disable control
        }
        if (dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y")
        {
            //Disable control
        }
        if (dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y")
        {
            //Disable control
        }
        if (dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            //Disable control
        }
    }

public static void DisableFormControl(ControlCollection ChildCtrls)
{
    foreach (Control Ctrl in ChildCtrls)
    {
        if (Ctrl is TextBox)
            ((TextBox)Ctrl).Enabled = false;
        if (Ctrl is DropDownList)
            ((DropDownList)Ctrl).Enabled = false;
        if (Ctrl is CheckBoxList)
            ((CheckBoxList)Ctrl).Enabled = false;
        if (Ctrl is Button)
            ((Button)Ctrl).Enabled = false;
    }
}

But what happening here is

Even if one condition is FALSE and other three are TRUE, it still disables the whole control. I dont know why its happening like this

UPDATE

ON Page load I am disabling every control and after that.

DisableFormControl(Form.Controls);

    if (Hid_Mode.Value != "M")
    {
        DataTable dtFill = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM WMS_MENU_RIGHTS WHERE MKEY = '" + Session["UserId"].ToString() + "' and MENU_MKEY = '" + Request.QueryString["menuid"].ToString() + "'", constr);
        sda.Fill(dtFill);

        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = true;
            txtdesc.Enabled = true;
            txtadd1.Enabled = true;
            txtadd2.Enabled = true;
            txtadd3.Enabled = true;
            ddlCountry.Enabled = true;
            check_desc.Enabled = true;
            btnSaveExit.Enabled = true;
            btnDelete.Enabled = false;
            btnClear.Enabled = true;
        }
        if (dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = true;
            txtdesc.Enabled = true;
            txtadd1.Enabled = true;
            txtadd2.Enabled = true;
            txtadd3.Enabled = true;
            ddlCountry.Enabled = true;
            check_desc.Enabled = true;
            btnSaveExit.Enabled = true;
            btnDelete.Enabled = false;
            btnClear.Enabled = true;
        }
        if (dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = false;
            txtdesc.Enabled = false;
            txtadd1.Enabled = false;
            txtadd2.Enabled = false;
            txtadd3.Enabled = false;
            ddlCountry.Enabled = false;
            check_desc.Enabled = false;
            btnSaveExit.Enabled = false;
            btnDelete.Enabled = false;
            btnClear.Enabled = false;
        }
        if (dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = true;
            txtdesc.Enabled = true;
            txtadd1.Enabled = true;
            txtadd2.Enabled = true;
            txtadd3.Enabled = true;
            ddlCountry.Enabled = true;
            check_desc.Enabled = true;
            btnSaveExit.Enabled = true;
            btnDelete.Enabled = true;
            btnClear.Enabled = true;
        }
    }

But still the control is disabled even if condition is true

3
Always use parameterized queries to avoid sql-injection attacks! Your code has serious vulnerability. - Esko
@Esko: Thanks for stating that, but any idea on how to deal with this logic ? - hud
Your example code does not actually call the disable-method so this is not verifiable example. We don't know what you are passing there or see your control hierarchy. I suspect that you disable some parent control and in the process the child control will be disable as well. Or some other related problem to control hierarchy. - Esko
There are couple of things that may be causing the problem, First make sure you get capital Y from record rather small case y. Secondly you are not enabling controls in given code if once disabled. If control is disabled in one case and would be enabled in other case then your logic will keep it disabled because you are not enabling it. Debugging the code would make it easy to find the root cause. - Adil
@Adil, esko: let me try and check with debugging - hud

3 Answers

3
votes

You have overlapping cases what I suggest is to enable all the controls and disable specific control under condition. I think you have to make some option exclusive like view and delete! as they cancel each other. This is something you can try that I made based on discussion with you and you may need to further tune it.

txtAbbreviation.Enabled = true;
txtdesc.Enabled = true;
txtadd1.Enabled = true;
txtadd2.Enabled = true;
txtadd3.Enabled = true;
ddlCountry.Enabled = true;
check_desc.Enabled = true;
btnSaveExit.Enabled = true;
btnDelete.Enabled = true;
btnClear.Enabled = true;

if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" || dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y")
{
    btnDelete.Enabled = false;
}
bool enableAll = false;
if(dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
    enableAll = true;
else
if(dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y")
    enableAll = false;

if(dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y" || dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y")
{
    txtAbbreviation.Enabled = enableAll;
    txtdesc.Enabled = enableAll;
    txtadd1.Enabled = enableAll;
    txtadd2.Enabled = enableAll;
    txtadd3.Enabled = enableAll;
    ddlCountry.Enabled = enableAll;
    check_desc.Enabled = enableAll;
    btnSaveExit.Enabled = enableAll;
    btnDelete.Enabled = enableAll;
    btnClear.Enabled = enableAll;
}

Keep in mind you can not have head and tail at the some time some options could not coexist like View and Delete in your case.

1
votes

You are looking through specific field in the dtFill. I assume you are looking for only 1 condition to be true. UPDATE: Used your original code. Quite Lengthy.

Try this:

        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" || dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = true;
            txtdesc.Enabled = true;
            txtadd1.Enabled = true;
            txtadd2.Enabled = true;
            txtadd3.Enabled = true;
            ddlCountry.Enabled = true;
            check_desc.Enabled = true;
            btnSaveExit.Enabled = true;
            btnDelete.Enabled = false;
            btnClear.Enabled = true;
        }
        if (dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = false;
            txtdesc.Enabled = false;
            txtadd1.Enabled = false;
            txtadd2.Enabled = false;
            txtadd3.Enabled = false;
            ddlCountry.Enabled = false;
            check_desc.Enabled = false;
            btnSaveExit.Enabled = false;
            btnDelete.Enabled = false;
            btnClear.Enabled = false;
        }
        if (dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            txtAbbreviation.Enabled = true;
            txtdesc.Enabled = true;
            txtadd1.Enabled = true;
            txtadd2.Enabled = true;
            txtadd3.Enabled = true;
            ddlCountry.Enabled = true;
            check_desc.Enabled = true;
            btnSaveExit.Enabled = true;
            btnDelete.Enabled = true;
            btnClear.Enabled = true;
        }
1
votes

As per setting some criteria, I have to check multiple conditions and then assign the Enabled and Disabled logic. So before assigning the logic, what I did was created a Matrix for every possible conditions.

Below is the Image of the matrix.

enter image description here

So considering each matrix in mind, I assigned the functionality like below

if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }
        if (dtFill.Rows[0]["ADD_FLAG"].ToString() == "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() == "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() != "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() == "Y")
        {
            btnSaveExit.Enabled = true; btnDelete.Enabled = true; btnClear.Enabled = true;
            txtAbbreviation.Enabled = true; txtadd1.Enabled = true; txtadd2.Enabled = true; txtadd3.Enabled = true;
            ddlCountry.Enabled = true; check_desc.Enabled = true; txtdesc.Enabled = true;
        }

        if (dtFill.Rows[0]["ADD_FLAG"].ToString() != "Y" && dtFill.Rows[0]["MODIFY_FLAG"].ToString() != "Y" && dtFill.Rows[0]["VIEW_FLAG"].ToString() == "Y" && dtFill.Rows[0]["DEL_FLAG"].ToString() != "Y")
        {
            btnSaveExit.Enabled = false; btnDelete.Enabled = false; btnClear.Enabled = true;
            txtAbbreviation.Enabled = false; txtadd1.Enabled = false; txtadd2.Enabled = false; txtadd3.Enabled = false;
            ddlCountry.Enabled = false; check_desc.Enabled = false; txtdesc.Enabled = false;
        }

@All:- Let me know if something is missing or logically incorrect