Bind a nullable bit field to the TextMode property of a Textbox in ASP.Net - 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%>'/>

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")%>'/>

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't set Visible attribute in ASP.NET Panels

I am having trouble with visible attribute of an ASP.NET Panel control. I have a page that calls a database table and returns the results in a datagrid.
Requirements
If some of the returned values are null I need to hide the image that's next to it.
I am using a Panel to determine whether to hide or show the image but am having trouble with the statement:
visible='<%# Eval("addr1") <> DBNull.Value %>'
I have tried these as well:
visible='<%# Eval("addr1") <> DBNull.Value %>'
visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'
Code is below:
<asp:TemplateField >
<ItemTemplate>
<%# Eval("Name")%>
<p>
<asp:Panel runat="server" ID="Panel1"
visible='<%# Eval("addr1") <> DBNull.Value %>'>
<asp:Image Id="imgHouse" runat="server"
AlternateText="Address" SkinId="imgHouse"/>
</asp:Panel>
<%# Eval("addr1") %><p>
</ItemTemplate>
</asp:TemplateField>
What am I doing wrong?
Edit
If I use visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'
I get the following error:
Compiler Error Message: CS1026: ) expected
try:
<%# String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem,"addr1").ToString()) #>
try comparing the result of the eval to blank as opposed to null.
This would probably be easier to accomplish with server side code.
Handle the RowDataBound event in your grid (assuming you're using gridview, for DataGrid it's ItemDataBound) and then do this:
public void grid1_RowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
if(e.Row.RowType == RowType.DataRow)
{
object itemFromDb = e.Row.DataItem; //you'll need to cast this to your type
Panel p = (Panel)e.Row.FindControl("myPanel");
if(itemFromDb.SomeItem == null)
p.Visible = false;
}
}
This is off of the top of my head, I might have a syntax error or 2 in there. But you get the idea.
Hmmm...
visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'
Should work. What error are you getting?

Resources