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>
Related
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 %>'
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Want to enable image button based on the value in the Gridview Field
I want to disable the gridview delete button if the status=1. 1 means already deleted.I am using the below code to disable the delete button it returns the error message "The server tag is not well formed.".
<asp:TemplateField ItemStyle-Width="20px" HeaderImageUrl="~/images/icn_trash.png" >
<ItemTemplate>
<asp:ImageButton ID="btn_delete" runat="server" Enabled="<%# (Eval("fld_status").ToString()=="0") ? "true" : "false" %>" ToolTip="Delete" OnClientClick="return confirm('Important Alert : Do you delete this item ?')" CommandName="del" CommandArgument='<%#Bind("fld_val_id") %>' ImageUrl="~/images/icn_trash.png" />
</ItemTemplate>
<ItemStyle Width="20px"></ItemStyle>
</asp:TemplateField>
Try escaping the quotes
Enabled="<%# (Eval(\"fld_status\").ToString()=="0") ? "true" : "false" %>"
you could also use single quotes like you did here
CommandArgument='<%#Bind("fld_val_id") %>'
I assume you should use ' here:
Enabled='<%# (Eval("fld_status").ToString()=="0") ? "true" : "false" %>'
Replace the " with ' and remove the "" around the booleans:
Enabled='<%# (Eval("fld_status").ToString()=="0") ? true : false %>'
There are 4 readiobutton inside the repeater and i'm trying to show the checked radiobutton from database value.
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IIF(Eval("ANSWER")==1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
Second approach
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# Eval("ANSWER")==1 ? true : false %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
and so on for the rest radiobutton. But, it showing the error Expression Expected error. Need help. !!
It looks like you've got your C# and VB.Net mixed. Your first example looks like VB, the second like C#. However, you've got a few problems in your VB implementation:
The equality operator in VB is =, not ==
You should use the IF operator, not the IIF function, which is obsolete
The correct code should be as follows:
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IF(Eval("ANSWER")=1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
A basic question but there is no such question on Stack Overflow (for ASP.NET)
<asp:TextBox ID="txtUserName" runat="server" Text=<% Session["UserName"] %> >
I did this a week ago, now something is wrong. It should be simple. I also tried <%= %>, it did not work either. Putting a single quote around '<% %>' gives binding error. Help
I normally hide the implementation details from the aspx code with a property:
.cs file
public string UserName { get { return Session["UserName"]; } }
.aspx
<asp:TextBox ID="txtUserName" runat="server" Text='<%= UserName %>' >
What I did was pulled the text box in C# code and set it text value to the session.
Example:
txtUserName.text = Session["UserName"];
Use it in one of the function which checks the session values or you can use in page_load function (the default function for every page)
Now I think your code should look like <asp:TextBox ID="txtUserName" runat="server" Text='<%# Session["UserName"] %>' >
I always forget the sintax for inline code, this information could be helpfull I think.
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