Using iif function in checkbox - asp.net

I am working in a project where I need to display data from database in a gridview. The gridview contains varchar fields could be whether YES or NO. So I want to display rather than the text, checkbox. When its YES check box will be checked, when its NO check box will be unchecked.
My asp.net
<asp:CheckBox ID="chkCheck" runat="server" Checked='<%#"IIF(Eval("Check")="YES",true,false)%>' Width="80px" Enabled="false"/>
I am getting this error:
Character constant must contain exactly one character.
Is there any alternative option would solve my issue?

You can simply use this (for VB.NET)
Checked='<%# Eval("Check")="YES" %>'
For null values try
Checked='<%# Convert.ToString(Eval("Check"))="Yes" %>'

I'm not sure about your error, but do you need the IIF? I'm no expert in ASP, I'm relatively new to it, but why can't you just do Checked='<%# Eval("Checked") == "yes" %>'? And beyond that what about Checked='<%# Item.Checked == "yes" %>'?

Performance hint: Use DataRowView instead of Eval within the GridView control:
Checked='<%# (((System.Data.DataRowView)Container.DataItem)["Check"].ToString() == "YES") ? true : false %>'

Related

If statement on aspx [duplicate]

This question already has answers here:
If statement in aspx page
(7 answers)
Closed 9 years ago.
New to asp.net ...
In DataList, ItemTemplate, I would like to check if value ("Exchange") in the DB is true.
If it's true I'd like to display a label with the text "True".
Please note Exchange is stored in the DB as
Was thinking about this, but no success yet.
<%#Eval("Exchange").ToString() == "True" ? "<asp:Label ID=\"Exchange\" runat=\"server\" Text=\"True"> </asp:Label>":""%>
Can anyone help?
Many thanks
The asp.net way of doing what you want to do is this.
<asp:Label ID="Exchange" runat="server" Text="True" Visible='<%# Eval("Exchange").ToString() == "True" %>' />
If Exchange is bool you do not require the ToString call
<asp:Label ID="Exchange" runat="server" Text="True" Visible='<%# Eval("Exchange") %>' />
I am not able to try it, but you can write the if for the text property. This should display a label with no text (therefore nothing visible) if the Eval("Exchange") returns something other than "True"
<asp:Label ID="Exchange" runat="server" Text='<%# Eval("Exchange")=="True" ? "True": "" %></asp:Label>'
The DataReader["Exchange"].ToString() itself will return a string "True" or "False" hence you dont need to use any condition for this:
<asp:Label ID=\"Exchange\" runat=\"server\" Text=\"<%#Eval("Exchange").ToString()%>\" </asp:Label>

Asp.Net - Write True/False on UI based on boolean value of checkbox

I am working on this form in VS2012 and SQL Server 2008R2. The table gets the bit value of true or false. In the Gridview itemtemplate, instead of displaying the empty checkbox I want to say "True or False". How can I do that? This is what I have done so far:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# (Eval("Banned").ToString())%>' />
You need to set the Text property of the CheckBox control, like this:
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# DisplayTruth(Eval("Banned").ToString()) %>'
Text='<%# DisplayTruthValue(Eval("Banned").ToString()) %>' />
Now in your code-behind, create the two methods, like this:
protected bool DisplayTruth(string banned)
{
if(banned == "0")
{
return true;
}
return false;
}
protected string DisplayTruthValue(string banned)
{
if(banned == "0")
{
return "True";
}
return "False";
}
I recommend this approach for the following reasons:
The markup does not contain conditional/ternary logic
It is easier to debug the logic versus embedded code blocks
You can leverage the power of Visual Studio compiler to catch syntax errors at compile-time versus run-time errors when the logic is embedded into the binding syntax of the markup
I found this information and it is working. Also I changed asp:checkbox to asp:label. However I want to display "No" instead of "False" so I changed "False" to "No" but still my app says "False".
<asp:Label ID="CheckBox1" runat="server" Text='<%# IIf(Convert.ToString(Eval("Banned")) = "", "No", Eval("Banned"))%>' />

ASP.NET checkbox value store in SQL Server

I using FormView in my application. I found that no value property in asp checkbox is available! Following code for my check box (it displays True/False values from SQL Server).
So I just need to be able to add True/False or "on" value from checkbox to database when I saves form.
Thanks for help.
<asp:CheckBox runat="server"
CssClass="checkbox"
Text="Medicine"
ID="lic_board_medicine_chk"
TextAlign="Right"
Checked='<%# Eval("lic_board_medicine_chk").ToString() == "True" ? true : false%>'/>
try changing Eval to BIND
<asp:CheckBox runat="server"
CssClass="checkbox"
Text="Medicine"
ID="lic_board_medicine_chk"
TextAlign="Right"
Checked='<%# Bind("lic_board_medicine_chk").ToString() == "True" ? "checked" : "" %>'/>

How do I set the Visible attribute in an ItemTemplate?

<asp:TemplateField HeaderText="Audio">
<ItemTemplate>
<asp:Image ID="playImage" runat="server"
ImageUrl="~/images/nextpg.gif"
Visible='<%# (Eval("available")=="Y") ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
In my query I am returning the "available" column which is populated with a letter of Y or N. For some reason the evaluation of this expression is never true. If I change it to != instead of == it will always be true. That leads me to believe the Eval("available")=="Y" is simply not evaluating as expected.
After much messing around, this finally worked:
<%# ((String)Eval("available")).Equals("Y") ? true : false %>
The issue seems to be that you can't use == but instead you must use the String.Equals() method. I'm not sure why but that's just the way it is.

Can I negate the value in an attribute that uses an "Eval"?

I'd like to set a button's enabled state to be the negation of a value.
So given this code:
<asp:CheckBox ID="DefaultChecked"
Checked='<%# Bind("IsDefaultMessage") %>'
Enabled="false"
runat="server" />
<asp:LinkButton ID="MakeDefaultButton"
runat="server"
CommandName="MakeDefault'
CommandArgument='<%# Bind("ResidentialInfoID") %>'
Text="Make Default" />
How can I make the LinkButton Enabled attribute false if IsDefaultMessage == true?
Use Eval instead of Bind. Bind is for two-way binding, i.e. for cases where you need to be able to save the data back to your data source.
When you use Bind, the compiled page will actually have generated code that uses Eval to set the value, plus some code to read out the value for saving. Because Bind is replaced with generated code, you can't use any extra logic with Bind.
<asp:CheckBox ID="DefaultChecked" Checked='<%# !(bool)Eval("IsDefaultMessage") %>' Enabled="false" runat="server" />
<asp:LinkButton ID="MakeDefaultButton" runat="server" CommandName="MakeDefault' CommandArgument='<%#Bind("ResidentialInfoID") %>' Text="Make Default"/>
If you can use Eval, it is just a method of the Control class. It's only special in that it needs to be in the context of a data bound block <%# ... %>. Other than that, you can basically treat the block like a regular <%= %> expression block:
<%# !(bool)Eval("IsDefaultMessage") %>
If you want to still Bind it (Eval isn't round-trip), than you'll need to negate it back and forth during databinding. You may not need to do this though, if you can just re-word the control. For example, if a check box, instead of labelling it "Is Not Default Message" to the user and negating it back and forth, than lable it "Is Default Message". Contrived example, but you get the idea.
In case someone is looking for an option with VB.Net
<asp:CheckBox ID="DefaultChecked" Checked='<%# NOT (Eval("IsDefaultMessage")) %>' Enabled="false" runat="server" />
I've never used Bind but my understanding is that it is similar to Databinder.Eval. Either way, both methods return objects so you need to cast it to a boolean before evaluating it.
<%# !Convert.ToBoolean(Bind("IsDefaultMessage") %>
Edit: Looks like this can't be done and using a SqlDataSource on the page would solve the problem. http://forums.asp.net/t/1009497.aspx.
As I recall (It's been a while), there's no particular magic in <%#Bind(. It's just #Bind( inside <%....%>. Which means you'd want:
<% ! #Bind("IsDefaultMessage") %>'
The code
Checked='<%# Eval("IsDefaultMessage").ToString().Length() > 4 %>'
will return true if IsDefaultMessage is false
Since "False".Length = 5 and "True".Length = 4

Resources