session check for disable & enable checkbox C# - c#

I'm beginner & I need help
I try this code for check the id for employee to enable or disable the checkbox depend on the id for employee but the checkbox was disabled even when I login with the same Id in the session
if (Session["employeeNo"] == "12345")
{
CheckBoxList1.Enabled = true;
}
else
{
CheckBoxList1.Enabled = false;
}

If u r using CheckBox instead of Checkboxlist ...Try this Code It will Work
if (Session["employeeNo"].ToString() == "12345")
{
CheckBox1.Checked = true; // for Check
CheckBox1.Enabled = true; // for Enable
}
else
{
CheckBox1.Checked = false; // for UnCheck
CheckBox1.Enabled = false; // for Disable
}

The Session["employeeNo"] gives you an object, if you compare this with a string value the result will be false always. You have to convert this to string in order to compare with a string value, change the condition like this:
if (Session["employeeNo"].ToString() == "12345")

Related

How to use the UIView.hidden property to change visibility of views

In my IOS application, I need to show a drop-down menu when the menu button is pressed and hide it when the menu button is pressed again. I tried changing the hidden status to false and true as in the code below however that doesn't seem to work.
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}
Thanks to anyone that helps!
It's just simple, try this:
In Swift:
yourView.isHidden = true //or false
In Objective-C:
yourView.hidden = YES; //or NO;
In C#:
yourView.Hidden = true; //or false;
In your case you are doing it right, but the problem is you are using only if in both cases. You have to use else if for second if condition in order to achieve the desired result.
Otherwise the second if condition will be always true and get executed you will see no effect of first if block.
For Your Case:
It should be like:
menuButtonActive = !menuButtonActive
DropMenu.Hidden = menuButtonActive
Hope this help you! :)
It must be simple
menuButtonActive = !menuButtonActive;
DropMenu.Hidden = menuButtonActive;
look at follow code , add a else
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
else if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}

How to read .csv file and enable or disable the checkbox by considering column status in C#

I have a .csv file like below;
Name,Age,Marks0,Marks 1,Marks2,Marks3
Amal,22,TRUE,FALSE,FALSE,FALSE
Nimal,30,TRUE,TRUE,FALSE,FALSE
Perera,19,TRUE,TRUE,FALSE,FALSE
Sunil,25,TRUE,TRUE,FALSE,FALSE
Amali,22,TRUE,TRUE,FALSE,FALSE
Ann,26,TRUE,TRUE,FALSE,FALSE
Chamath,27,TRUE,FALSE,FALSE,TRUE
Kalana,29,TRUE,FALSE,FALSE,TRUE
Tom,25,TRUE,FALSE,FALSE,TRUE
Jerry,22,TRUE,FALSE,FALSE,TRUE
Peter,23,TRUE,FALSE,FALSE,TRUE
So, I want to read this .csv file and check whether the status of Marks0, Marks1, Marks2, Marks3. After that, I want to enable or disable checkbox during the button1 click to browse this file.my interface is like this.
Interface of my code
In here want to check status of Marks0, Marks1, Marks2, Marks3
the condition is like this, that selected column whole data are TRUE then enable the checkBox.but there are True and False are there then checkBox is enabled.but all are False then checkBox is Disable.
An example in here
Marks0 all are TRUE then checkBox1 is enable
Marks1 all are TRUE and FALSE then checkBox2 is enable
Marks2 all are FALSE then checkBox3 is disable
Marks3 all are TRUE and FALSE then checkBox4 is enable
like that, I want to build my code.
I can enable the checkBox whole column is TRUE and I can disable the checkBox whole column is FALSE but I can not enable the checkBox that it is TRUE and False like Marks1 and Marks3.please give me a solution for this.
My code is following;
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "*.csv|*.csv";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //"C:\\BA2000";
fileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (ofd.ShowDialog() == DialogResult.OK)
{
tbOutputFilePath.Text = ofd.FileName;
fileOriginalOutputPath = tbOutputFilePath.Text;
if (tbOutputFilePath != null)
{
List<Marks> ObservingData = new List<Marks>(); // List to store all available Marks objects from the CSV
Marks statusInt = new Marks();
// Loops through each lines in the CSV
foreach (string line in System.IO.File.ReadAllLines(tbOutputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
{
// here line stands for each line in the CSV file
string[] InCsvLine = line.Split(',');
statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);
}
if (statusInt.Mark0 == false)
{
checkBox1.Enabled = false;
}
if (statusInt.Mark1 == false)
{
checkBox2.Enabled = false;
}
if (statusInt.Mark2 == false)
{
checkBox3.Enabled = false;
}
if (statusInt.Mark3 == false)
{
checkBox4.Enabled = false;
}
}
}
}
}
I created a class to store my column value
class Marks
{
public string Name { get; set; } // property to store Name
public int Age { get; set; } // property to store Age
public bool Marks0 { get; set; } // property to store Marks0
public bool Marks01 { get; set; } // property to store Marks01
public bool Marks2 { get; set; } // property to store Marks2
public bool Marks3 { get; set; } // property to store Marks3
}
Edit:
Let me see if I understood you correctly:
if any (or all) Mark0 is true then the checkbox1 is enabled. If ALL are false checkbox is disabled. Same rule apply to other marks and checkboxes.
First, you're missing adding statusInt to your ObservingData collection. Next, remove all those if (statusInt.Mark0 == false) checks because they are checking onyl the lastest row that you have read from csv. To sum things up, this is what needs to be done:
foreach (string line in System.IO.File.ReadAllLines(tbOutputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
{
// here line stands for each line in the CSV file
string[] InCsvLine = line.Split(',');
//init Marks class
Marks statusInt = new Marks();
statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);
//add line read from csv to colletion
ObservingData.Add(statusInt);
}
//instead of if (statusInt.Mark0 == false), if (statusInt.Mark1 == false) etc, add this
checkBox1.Enabled = ObservingData.Any(m => m.Marks0);
checkBox2.Enabled = ObservingData.Any(m => m.Marks1);
checkBox3.Enabled = ObservingData.Any(m => m.Marks2);
checkBox4.Enabled = ObservingData.Any(m => m.Marks3);

How to check if a GridView contains a certain Button asp.net c#

hi i would like to know how can i check if any of the reviewBtn is visible in the gridview and if ANY reviewBtn is present, btn_reviewAll will be visible.
currently the code below only shows the btn_reviewAll when ALL reviewBtn is visible. pls advise thanks!
foreach (GridViewRow row in GridViewReview.Rows)
{
Control reviewBtn = row.FindControl("ButtonReview") as Button;
if (reviewBtn.Visible == true)
{
btn_reviewAll.Visible = true;
}
else
{
btn_reviewAll.Visible = false;
}
}
change your code like this
foreach (GridViewRow row in GridViewReview.Rows)
{
Control reviewBtn = row.FindControl("ButtonReview") as Button;
if (reviewBtn.Visible == true)
{
btn_reviewAll.Visible = true;
break;
}
else
{
btn_reviewAll.Visible = false;
}
}
what it does is when one reviewBtn is visible it will set btn_reviewAll to visible and break out the foreach loop
#Shreesha's answer is absolutely correct, you can also do with less code using LINQ like this:-
if (GridViewReview.Rows.OfType<GridViewRow>()
.Any(b => ((Button)b.FindControl("ButtonReview")).Visible))
btn_reviewAll.Visible = true;
else
btn_reviewAll.Visible = false;

Insert value into radiobutton

I'm trying to insert a value into a RadioButton based on a value I have in an array.
This is what I'm trying to acheive, but I can't seem to get it work.
I know the IsChecked is a check if the RadioButton is checked or not, but I want to describe the means.
if (arrAnswer[nAnswerNum] == 0)
{
radioTrue.IsChecked = false;
radioFalse.IsChecked = false;
}
else if (arrAnswer[nAnswerNum] == 1)
{
radioTrue.IsChecked = true;
}
else if (arrAnswer[nAnswerNum] == 2)
{
radioFalse.IsChecked = true;
}
Thanks
Do it like this, it's much simpler
radioTrue.IsChecked = arrAnswer[nAnswerNum] == 1;
radioFalse.IsChecked = arrAnswer[nAnswerNum] == 2;
also dont forget to check if the index does exists to prevent indexoutofrange exception

Select and unselect checkboxes

i have a list of Checkbox's .
checkbox1,checkbox2....
i want to uncheck the checkbox if its is checked and vice versa.Is there any way to do this.
foreach (CheckBox cb in cbList) {
cb.Checked = !cb.Checked;
}
if (CheckBox1.Checked == true)
{
CheckBox1.Checked = false
}
else
{
CheckBox1.Checked = true
}
I don't know if you're working in ASP.NET, WPF, WinForms, ...
But it's as easy as check if checkbox is checked, then uncheck, and viceversa.
Or you can implement an extension method like:
public static class CheckboxExtensions
{
public static void ToggleChecked(this CheckBox some)
{
if(some != null)
{
if(some.Checked)
{
some.Checked = false;
}
else
{
some.Checked = true;
}
}
}
}
... And your actual code will look like this:
chkSome.ToggleChecked();
Try
checkBox1.Checked = !checkBox1.Checked;

Categories