Setting asp:label text colour based on text - c#

I am databinding a gridview and one of the columns is defined as shown below. What I would like to do is to colour the text depending on whether the text says "Yes" or "No". If the text is "Yes" I'd like to set it to red else set it to green. Can this be done and if so should it be done via css or can I add some code to the line?
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' />
</ItemTemplate>
</asp:TemplateField>

The below should do what you want.
ASP.NET
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' CssClass='<%# SetColor(DataBinder.Eval(Container.DataItem, "Validation")) %>' />
</ItemTemplate>
C#
public string SetColor(string Text)
{
return Text.ToUpper == "YES" ? "GreenClass" : "RedClass"
}
CSS
.GreenClass{color:green;}
.RedClass{color:red;}

Related

how to make a read only textbox in gridview edit mode?

I want to make one of the data bound in my gridview uneditable or in my case, I want to make the textbox in edit mode in read only. here is what I have tried but not successful:
TextBox ProductImage = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
ProductImage.ReadOnly = true;
and here is the aspx code:
asp:TemplateField HeaderText="ProductImage" SortExpression="ProductImage">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductImage") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' />
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
can someone help me out?
Have you tried setting ReadOnly to true in your aspx code?
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text='<%# Eval("ProductImage") %>'/>
</EditItemTemplate>
Or you could use a Label instead of a TextBox
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductImage") %>'/>

Telerik RadGrid Column Filtering on Text Value of LinkButton

<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
So how can I allow RadGrid to filter the results based on the text value of LinkButton? Is there a way to do that?
Please try with the below code snippet.
<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True"
DataField="Code">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I have added DataField property in template column. if you get any error in grid then please set EnableLinqExpressions="false" in your grid.

Not able to DataBound the `Style` attribute in GridView

Not able to DataBound the Style attribute
Style='<%# Eval("LeftPadding","padding-left:{0}") %>'
Full Code
<asp:TemplateField HeaderText="Report Item" SortExpression="ReportItem">
<ItemTemplate>
<asp:Label Style='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="350px" />
</asp:TemplateField>
But I can DataBound some other attributes even not a Standard HTML attribute, like below
<asp:Label StyleTemp='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
What is the problem with Style ?
Got Answer :)
<asp:Label Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
ID="lblReportItem" runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
Tried by without giving the Single Quotes for the style attribute and used string.Format
Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>

Add column with static text on gridview

I am working on an asp.net project. How can I add a static column with static text to a GridView which is loaded with a dataset? For example I want a column Check which has values in every row Checked.
<asp:TemplateField HeaderText="YouText">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cb" Checked='<%# DataBinder.Eval(Container, "DataItem.isChecked") %>' />
</ItemTemplate>
</asp:TemplateField>
Or if you just want static text do this:
<asp:TemplateField HeaderText="YouText">
<ItemTemplate>
Your text
</ItemTemplate>
</asp:TemplateField>
<asp:GridView ID="gridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
//static text or checkbox
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
gridView.DataSource = dataset;
gridView.DataBind();
Your static text will show every rows.
So you want the static text with the dynamic bool?
You can use a binding expression.
<ItemTemplate>
Check
<asp:CheckBox ID="myCheckbox" runat="server"
Checked='<%# Eval("IsChecked") %>'
AutoPostBack="true"
OnCheckedChanged="myCheckbox_Checked" />
</ItemTemplate>

format decimal value in gridview

I have a bound field in my Gridview which is getting its value from a database table.
I have got the data but don't know how to format it inside the gridview.
For example I get total data from below like "123456", but I want to display as "123,456"
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" />
How can I do this? Do I need to convert the bound field into a template field ? But what do i do after that.
please help.
I have used DataFormatString="{0:n0}" and it solved the above problem.
how do i do for this:
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
the hyperlink has the number which need to be formatted...
Use DataFormat property :
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" DataFormatString="{0:n3}" />
EDIT : For the second part of your question use Eval method's second parameter to format your data :
<%# Eval("NumFailedFiles", "{0:n3}") %>
Then your template will be like that :
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server"
ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>'
Text='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
A couple of ways in how you can do that
Option 1
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles","{0:n0}") %>'></asp:HyperLink>
Option 2
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles").ToString("N") %>'></asp:HyperLink>
Option 3
Create a method in the code behind page that returns the formatted number.
protected string GetFormatedNumber(object number)
{
if ( number != null )
{
return number.ToString("N");
}
return "0";
}
And call the method in your aspx page as below:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%#GetFormatedNumber(Eval("NumFailedFiles")) %>'></asp:HyperLink>
I think you need to take a look at this MSDN article on How to Format Data in the DataGridView
if you want to format your data on gridview use "{0:n3}"
<asp:Label ID="label" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "data","{0:n3}") %>'></asp:Label>

Categories