ASP.Net to return Yes or No (instead of True / False)

I am using C # and ASP.Net 3.5 and trying to set the value to "Yes" / "No" from the checkbox, not True / False. Is there an easy way or do I need to make if statements?

+3
source share
7 answers

be sure to try the following:

string doesThisWork = chkBox.Checked ? "Yes":"No"

more details ...

+9
source

How about adding an extension method for the CheckBox class for this:

public static class CheckBoxExtensions
{
    public static string IsChecked(this CheckBox checkBox)
    {
        return checkBox.Checked ? "Yes" : "No";
    }
}

Using:

var checkBox = new CheckBox();
checkBox.Checked = true;

Console.WriteLine(checkBox.IsChecked());
// Outputs: Yes
+5
source
string YesNo = chkYesNo.Checked ? "Yes" : "No";
+1

, , .

-

string answer = checkbox.Checked ? "Yes" : "No";

.

- Yes/No direct ( ), , true/false . , , "" / "" - , , , .

+1

:

checkbox.Checked ? "Yes" : "No"

, :

static readonly Dictionary<bool, string> BooleanNames = new Dictionary<bool, string> { 
    { true,  "Yes" },
    { false, "No"  }
};

BooleanNames[checkbox.Checked]

.

0

I assume that you are using the asp.net control and are viewing the Verified property. If so, you will need instructions to translate the boolean value to yes / No:

string yesNo = checkbox_control.Checked ? "Yes" : "No";
0
source

Just to give you an alternative to no if (maybe this is not the best approach in this case, but ...):

Dictionary<Boolean, String> strings = new Dictionary<Boolean, String>();
strings.Add(true, "Yes");
strings.Add(false, "No");

Then when you need the value:

String yesNo = strings[checkbox.Checked];
0
source

Source: https://habr.com/ru/post/1736712/


All Articles