Display particular word on some condition Eval Asp.net - c#

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

Related

eval in if statement design view

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>

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>

Set the values to Null if its a 0 on Data Grid asp.net

I have a Data grid that has Item Templates with Labels in it:
<asp:datagrid id="ID" runat="server" Width="641px" CellPadding="2" PageSize="2" DataKeyField="IDs"
AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="ID_ItemDataBound" >
<SelectedItemStyle ForeColor="HighlightText" BackColor="Highlight"> </SelectedItemStyle>
<AlternatingItemStyle BackColor="WhiteSmoke">
</AlternatingItemStyle>
<HeaderStyle Font-Bold="True" BackColor="AliceBlue">
</HeaderStyle>
<FooterStyle Font-Bold="True" BackColor="AliceBlue">
</FooterStyle>
<Columns>
<ItemTemplate>
<asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>' runat="server" ID="dataScore" Text='<%# DataBinder.Eval(Container, "DataItem.dataScore" ) %>'>
</asp:label>
<asp:label BorderStyle=None Text='<%# GetCompleteIncomplete(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.dataScore")!=null)) %>' Visible='<%# DataBinder.Eval(Container, "DataItem.IsCompleteOrNot") %>' id="txtIsComplete" runat="server">
</asp:label>
</ItemTemplate>
</columns>
And I am trying to Set the 0s to blank instead of showing 0 in the DataGrid so on ItemData bound I get the Label like this and trying to set the value to null:
if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
Label dataScore = (Label)e.Item.FindControl("dataScore"); // Gets that Label
Label txtIsComplete = (Label)e.Item.FindControl("txtIsComplete");
if(dataScore .Text == "0")
{
dataScore.Text = string.Empty; // Tried
}
You can write a function to analyse the value of DataBinder.Eval
Private String MyFunction(String value)
{
Return value == "0" ? String.Empty : value;
}
<asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>' runat="server" ID="dataScore" Text='<%# MyFunction(DataBinder.Eval(Container, "DataItem.dataScore" )) %>'>

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.

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

Categories

Resources