How do I set the Visible attribute in an ItemTemplate? - asp.net-2.0

<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.

Related

Using iif function in checkbox

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 %>'

ASP.NET Databinder if else conditional asp control

I have a object with value true or false. If the value is false, I want to display a asp:Button control, else it displays nothing. Can this be accomplished?
I want something like this:
<%# DataBinder.Eval(Container.DataItem, "FullyPaid").Equals(false) ? "<asp:Button Text=\"Pay Now\"/>" : ""%>
I did worked like same here is what I used to play with Visible property:
Visible='<%#(Convert.ToInt32(Eval("pricetype")) == 1) ? true : false%>'
Try it the other way around:
<asp:Button ID="btnPayNow" runat="server" Text="Pay Now" Visible='<%# DataBinder.Eval(Container.DataItem, "FullyPaid")%>'/>
Simply, but you must be sure that FullyPaid must contain either true or false.
<asp:Button ID="btnPayNow" runat="server" Text="Pay Now"
Visible='<%#Eval("FullyPaid")%>'/>

Bind a nullable bit field to the TextMode property of a Textbox in ASP.Net

I have a table with a list of questions. One of the properties is largeText.
NULL means hide the textbox
TRUE means show a MultiLine textbox
FALSE means show a single line textbox.
Here's what I am trying to do
<asp:TextBox ID="tbxFreeResponse" runat="server"
Visible='<%# Eval("largeText") != null %>'
TextMode = '<%# (Eval("largeText") == (object)true) ?
TextBoxMode.SingleLine :
TextBoxMode.MultiLine%>'/>
The Eval("largeText") == (object)true always evaluates to FALSE though. What am I missing? The (object) cast is necessary because otherwise it complains about type incompatibility.
Here's what did the trick for me, hopefully someone will find this useful.
<asp:TextBox ID="tbxFreeResponse" runat="server"
Visible='<%# Eval("largeText") != null %>'
TextMode = '<%# (!(Eval("largeText") is DBNull) && (bool)Eval("largeText")) ?
TextBoxMode.MultiLine :
TextBoxMode.SingleLine%>'/>

Set value for 'visible' property in ASPX page programatically

I am trying to set the visible property for a label to either true or false depending on a condition. This is in ASPX page. I am doing something wrong and getting error when this is executed.
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED'
Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") > 0%>'>
</asp:Label></td>
Error I am getting is below.
Compiler Error Message: CS0019: Operator '>' cannot be applied to
operands of type 'object' and 'int'
What changes need to be done?
All I need to do set the visible property of the LABEL to true when 'IsAuthorized' is greater than zero.
That's because you have a syntax error, you silly bunny.
Here you are, it should be like this:
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") %>' /></td>
You had an extra > and a 0 in there somewhere.
Also, since you aren't doing anything between the <asp:Label and </asp:Label>, you can close it with an end slash and skip a separate ending tag. Like this <asp:Label ... />
ALSO, sometimes trying to set a visible property like that causes problems, the program can complain that the value wasn't a Boolean. You might want to also ad an explicit conversion like this:
Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsAuthorized")) %>'
Assuming that IsAuthorized is a bit type, just cast it to a boolean:
Visible='<%#Convert.ToBoolean(Eval("IsAuthorized"))%>'
Note on a server side control you can do this:
<someControl id="myId" runat="server" Visible='<%# this.SomeField > 5 %>'>
But it won't work unless you call DataBind in the code behind, such as in Page_Load:
myId.DataBind():
Assuming IsAuthorized is an integer, you should use this:
Visible='<%# ((int)DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>'
Eval returns an object, so you have to cast it to an integer first.
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# (int)(DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>' ></asp:Label></td>

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