eval in if statement design view - c#

I have a repeater showing product stocks. I want show if no stock "Out of stock". If present show stock amount and unit.
I tried the following ways:
<%#Convert.ToInt32(Eval("AMOUNT")) == 0 ? "Out of stock" : %><%#Eval("AMOUNT") %> <%#Eval("UNIT") %>
and
<% if ( Convert.ToInt32(Eval("AMOUNT")) == 0) { %>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
<%} else { %>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("AMOUNT") %>'></asp:Label>
<% } %>
I am getting this error in this method:
System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Maybe there is a simple solution but I can't find. Thanks.

This if condition obviously doesn't support data binding, hence InvalidOperationException occurs:
<% if ( Convert.ToInt32(Eval("AMOUNT")) == 0) { ... } %>
Since the if condition has two blocks of markup (with else condition), you can use two asp:PlaceHolder controls as replacement with different visibility condition (one equals to zero and other is greater than zero):
<asp:PlaceHolder ID="AmountPlaceHolder1" runat="server" Visible='<%# Eval("AMOUNT") == 0 %>'>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
</asp:PlaceHolder>
<asp:PlaceHolder ID="AmountPlaceHolder2" runat="server" Visible='<%# Eval("AMOUNT") > 0 %>'>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("AMOUNT") %>'></asp:Label>
</asp:PlaceHolder>
Or use strongly-typed RepeaterItem.ItemType property instead of Eval:
<asp:PlaceHolder ID="AmountPlaceHolder1" runat="server" Visible='<%# Item.Amount == 0 %>'>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
</asp:PlaceHolder>
<asp:PlaceHolder ID="AmountPlaceHolder2" runat="server" Visible='<%# Item.Amount > 0 %>'>
<asp:Label ID="Label2" runat="server" Text='<%# Item.Amount %>'></asp:Label>
</asp:PlaceHolder>

Related

How can i use If condition in aspx page for image url?

My dataset return this table link.How can i use If else conditons in aspx page?I want if the File_path is Null or empty then image not show because if File_Path is empty then it give me error,I want if File_Path is not empty then image will be show.Following error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is my code
<asp:DataList ID="DataList2" runat="server" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div class="<%#GetStyleForMsgList(Eval("MsgSender").ToString()) %> MainChatListClass">
<asp:Label ID="Label1" runat="server" Text='<%# GetPerfactName(Eval("MsgSender").ToString()) %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ChatMsg") %>'></asp:Label>
<asp:Label ID="lblfile" runat="server" Visible ="false" Text='<%# Eval("File_Path") %>'></asp:Label>
<% if (Eval("File_Path") != null)
{%>
<asp:Image ID ="img" runat ="server" ImageUrl ='<%# Eval("File_Path") %>' />
<% } %>
</div>
</ItemTemplate>
</asp:DataList>

Return NULL JavaScript

I don't understand why I am getting alert "Empty" when I type some numbers or leave a empty? Can you help me?
My Code:
</script>
<script type="text/javascript">
function tell() {
var v = true;
if (document.getElementsByName("TextBox33").length == 0)
alert("Empty");
v = false;
return v;
}
</script>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" OnClientClick="javaScript: return tell();"
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
I am adding more html:
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="286px" OnItemInserting="DetailsView1_ItemInserting" OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted">
<asp:TemplateField HeaderText="IMEI" SortExpression="imei">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" CssClass="TextBox33" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("imei") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You're calling getElementsByName which returns al array of DOM elements. You're then taking the length of that, which will always be more than 0 because the element exists.
Try adding a [0] after the getElementsByName call, and then change .text to .value.
Also so you're aware, you need to put the alert and v = false inside brackets. You can only skip brackets if you're executing one statement.
Here's the revised code:
if (document.getElementById("TextBox33").value.length == 0){
alert("Oh no I'm empty!");
v = false;
}
Update: I also just noticed that you didn't actually give the textbox a name of TextBox33, you gave it an ID. I've updated the code.

Display particular word on some condition Eval Asp.net

<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text='<%# Eval("Leave_Status").ToString() == 'A' ? 'Approved' : 'Applied' %>'></asp:Label>
Condition to display a particular word in the GridView on some condition
Could some thing like this work on Gridview
I get an error Server Tag not well formed
Try this
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text='<%# Eval("Leave_Status").ToString() .Equals ( "A") ? "Approved" : "Applied" %>'></asp:Label>
Use Below Code:
<% if(Eval("Leave_Status").ToString() == "A"{%>
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text="Approved"></asp:Label>
<%}
else {
%>
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text="Applied"></asp:Label>
<%}%>

Change List Colour according to date

I have a DataList which I am binding on load, which works perfectly fine. My question is how do I make this to show records that don't have a date in database, in different text colour? here is my code:
<asp:DataList ID="dlS" runat="server" EnableViewState="false">
<ItemTemplate>
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' /><br />
</ItemTemplate>
</asp:DataList>
Guid ID = (Guid)Session["ID"];
lstL = Manager.Get_ByID(ID);
if (lstLetters != null)
{
dlS.DataSource = lstL;
dlS.DataBind();
}
I'm not sure what you really mean by "records that don't have a date in database",
but if it means that those records have NULL value as date then you can rewrite the Label as below:
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' ForeColor='<%# Eval("DateValue") == System.DBNull ? System.Drawing.Color.Red : System.Drawing.Color.Blue %>' />

Accessing cell 1 in Telerik RadGrid from cell 3 (not databound)

My grid is 5 columns, an "Event" column followed by Q1 through Q4. One of the quarter columns is editable at any given time, the other three are labels. Here is the code I use to determine which to show (q defined elsewhere):
<telerik:GridTemplateColumn HeaderText="Q2" DataField="Q2" UniqueName="Q2">
<ItemTemplate>
<% If (q = 3) Then%>
<telerik:RadNumericTextBox ID="rntb_Q2" runat="server" Type="Number" ShowSpinButtons="true" Value='<%# CInt(Eval("Q2")) %>' Width="50px" CssClass="short" MinValue="0">
<IncrementSettings InterceptArrowKeys="false" InterceptMouseWheel="false" Step="1" />
<NumberFormat DecimalDigits="0" GroupSeparator="," />
</telerik:RadNumericTextBox>
<% Else%>
<asp:Label ID="lbl_Q2" runat="server" Text='<%# If(Eval("Q2") Is Nothing, "0", Eval("Q2")) %>'></asp:Label>
<% End If %>
</ItemTemplate>
</telerik:GridTemplateColumn>
This code all works fine; the problem is when I want to change one row to display a label all the time. I tried If ((q = 3) AndAlso (Not GetDataItem("EventDesc").ToString.ToLower = "total error doses")) as well as If ((q = 3) AndAlso (Not Eval("EventDesc").ToString.ToLower = "total error doses")) but with both I receive an error about not being able to use Eval, etc outside of data bound.
Thank you for your help.
So what I was trying to do was change whether a RadNumericTextBox or Label was displayed. This achieves the result without accessing the contents of another cell directly. It uses the .ItemIndex property to determine what controls should be visible.
<telerik:GridTemplateColumn HeaderText="Q2" DataField="Q2" UniqueName="Q2">
<ItemTemplate>
<% If (q = 3) Then %>
<telerik:RadNumericTextBox ID="rntb_Q2c" runat="server" Type="Number" ShowSpinButtons="true" Value='<%# Eval("Q2") %>' Width="75px" CssClass="short" Visible='<%#If(TryCast(Container, GridDataItem).ItemIndex < 9, True, False) %>'>
<IncrementSettings InterceptArrowKeys="false" InterceptMouseWheel="false" Step="1" />
<NumberFormat DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
<asp:Label ID="lbl_Q22" runat="server" Text='<%# Eval("Q2") %>' Visible='<%#If(TryCast(Container, GridDataItem).ItemIndex < 9, False, True) %>'></asp:Label>
<% Else %>
<asp:Label ID="lbl_Q2" runat="server" Text='<%# If(Eval("Q2") Is Nothing, "0", Eval("Q2")) %>'></asp:Label>
<% End If %>
</ItemTemplate>
</telerik:GridTemplateColumn>

Categories

Resources