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
Related
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;
}
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")
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;
I'm just getting the hang of Lightswitch, but I keep getting the null reference exception error when I'm trying to find out if a selected item in a datagrid contains the letters "CMP". I look all over the place but I think I'm doing something wrong. Here is my code for reference:
if(string.IsNullOrWhiteSpace(this.location.SelectedItem.locationID))
{
this.ShowMessageBox("test"); //not sure what to put there so I just made something up
}
else if (this.location.SelectedItem.locationID.Contains("CMP"))
{
this.FindControl("datePurchased").IsVisible = true;
this.FindControl("age").IsVisible = true;
this.FindControl("userList").IsVisible = true;
}
else
{
this.FindControl("datePurchased").IsVisible = false;
this.FindControl("age").IsVisible = false;
this.FindControl("userList").IsVisible = false;
}
I also tried
if(this.location.selecteditem.locationID != null)
if(string.IsNullOrEmpty)
but it always throws me the same error. Any help would be much appreciated!
I guss this.location or this.location.selecteditem may be null, So you got that error.
So please try this if condition instead of your if condition way
if(this.location != null && this.location.selecteditem !=null && this.location.selecteditem.locationID != null)
{
//Write your code here
}
So your final code look like
if(this.location == null && this.location.selecteditem ==null && this.location.selecteditem.locationID == null)
{
this.ShowMessageBox("test"); //not sure what to put there so I just made something up
}
else if (this.location.SelectedItem.locationID.Contains("CMP"))
{
this.FindControl("datePurchased").IsVisible = true;
this.FindControl("age").IsVisible = true;
this.FindControl("userList").IsVisible = true;
}
else
{
this.FindControl("datePurchased").IsVisible = false;
this.FindControl("age").IsVisible = false;
this.FindControl("userList").IsVisible = false;
}
To find an item (and select it) in a dropdownlist using a value we simply do
dropdownlist1.Items.FindByValue("myValue").Selected = true;
How can I find an item using partial value? Say I have 3 elements and they have values "myValue one", "myvalue two", "myValue three" respectively. I want to do something like
dropdownlist1.Items.FindByValue("three").Selected = true;
and have it select the last item.
You can iterate from the end of the list and check if value contains the item (this will select the last item which contains value "myValueSearched").
for (int i = DropDownList1.Items.Count - 1; i >= 0 ; i--)
{
if (DropDownList1.Items[i].Value.Contains("myValueSearched"))
{
DropDownList1.Items[i].Selected = true;
break;
}
}
Or you can use linq as always:
DropDownList1.Items.Cast<ListItem>()
.Where(x => x.Value.Contains("three"))
.LastOrDefault().Selected = true;
You can iterate the items in your list, and when you find the first one whose items's string contains the pattern, you can set its Selected property to true.
bool found = false;
int i = 0;
while (!found && i<dropdownlist1.Items.Count)
{
if (dropdownlist1.Items.ToString().Contains("three"))
found = true;
else
i++;
}
if(found)
dropdownlist1.Items[i].Selected = true;
Or you could write a method (or extension method) that does this for you
public bool SelectByPartOfTheValue(typeOfTheItem[] items, string part)
{
bool found = false;
bool retVal = false;
int i = 0;
while (!found && i<dropdownlist1.Items.Count)
{
if (items.ToString().Contains("three"))
found = true;
else
i++;
}
if(found)
{
items[i].Selected = true;
retVal = true;
}
return retVal;
}
and call it like this
if(SelectByPartOfTheValue(dropdownlist1.Items, "three")
MessageBox.Show("Succesfully selected");
else
MessageBox.Show("There is no item that contains three");
Above mentioned answers are perfect, just there are not case sensitivity proof :
DDL.SelectedValue = DDL.Items.Cast<ListItem>().FirstOrDefault(x => x.Text.ToLower().Contains(matchingItem)).Text