How to conditionally generate html in ASP template - c#

I'm a total newbie to this. How do I conditionally wrap this <Colosseum:PersonLabel... with <strong></strong>
<asp:TemplateColumn
HeaderText="Member"
ItemStyle-Wrap="false"
SortExpression="leader_name">
<ItemTemplate>
<Colosseum:PersonLabel ID="plPerson" runat="server"
PersonGUID='<%# Eval("person_guid") %>'
PersonName='<%# Eval("person_name") %>'
HasPhoto='<%# Eval("person_blob_id") != DBNull.Value %>'
Restricted='<%# Eval("restricted") != DBNull.Value && Convert.ToBoolean(Eval("restricted")) %>'
/>
</ItemTemplate>
</asp:TemplateColumn>

For example:
Font-Bold='<%# Eval("FeedName").ToString().Contains("Band") %>'
It works essentially like the other expressions you have on the label.
// true if `person_blob_id` is not null.
HasPhoto='<%# Eval("person_blob_id") != DBNull.Value %>'

Related

Eval function for checkbox

This is my html code inside the gridview.. I want to add "OR" condition in Eval function..
how can i do?
<asp:TemplateField HeaderText="Report" HeaderStyle-CssClass="headerfont">
<ItemTemplate>
<asp:CheckBox ID="checkReport" runat="server" Checked='<%# bool.Parse(Eval("rm_rights").ToString() == "1R" ? "True": "False") %>'
Enabled="true" AutoPostBack="True" />
</ItemTemplate>
<HeaderStyle CssClass="headerfont"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
i need code like this
`Checked='<%# bool.Parse(Eval("rm_rights").ToString() == "2R" ? "True": "False") %>'`
OR operator
Checked='<%# bool.Parse(Eval("rm_rights").ToString() == "3R" ? "True": "False") %>'
you are using bool function and u evaluate this expression with 1R so it gives u error .
remove bool.parse function and check again
Try this , You have misplaced the bracket ,<%# ... %> block supports conditional operator, eval is a different function
" <%# Eval("rm_rights").ToString() == "1R" ? "True": "False" %>"

Conditional Eval in Gridview Template row

I am having an issue that I am certain is easy to resolve, I just don't know what to do.
Here is my code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Literal ID="text_shipped" Text="Media Shipped" runat="server" />
<br />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />--></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />--></ItemTemplate>
</asp:TemplateField>
The label "lbl_shipped" is showing the correct value which is either "Yes" or "No"
but, i want to add a button "lnk_ship", based on whether
or not the value is "Yes" (show button), or "No" (do not show button).
My issue is that I am using conditional code on the Visible keyword and I am testing for the value, but it seem to be ignoring my value for "shipped"
here are the main two lines, the first one shows the value, the second line is conditional, the conditional is NOT working. it keeps showing false:
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />
DataBinder.Eval(Container.DataItem, "shipped" ).ToString()
Add .ToString() ie:
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ).ToString() == "Yes" ? true : false %>' />
I have just mocked up something quickly and it is working for me (ASP.NET web forms targeting .NET 4, VS2012) , maybe have a look:
Default.aspx
Contains the following GridView definition which I stuck randomly in a new web forms project.
<asp:GridView runat="server" ID="gridMe" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Button runat="server" ID="btnName" Text="Hi" Visible='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() == "Bob" %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs
Has the following class definitions
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<Thing>
{
new Thing() {ID = 1, Name = "Bob"},
new Thing() {ID = 2, Name = "Geraldine"}
};
gridMe.DataSource = list;
gridMe.DataBind();
}
}
public class Thing
{
public int ID { get; set; }
public string Name { get; set; }
}
Result
My output is something like:
ID Name
1 [Hi]
2

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

hide gridview hyperlink in visible property of source rather then code behind

Ive got a gridview with an itemtemplate that has a hyperlink control in it. I want to hide a hyperlink control if its item in the database returned null:
<ItemTemplate>
<asp:HyperLink ID="hlSugar" Visible=<% DataBinder.Eval(Container, "DataItem.CaseID")==null %> ToolTip="View the issue in SugarCRM." Target="_blank" runat="server" NavigateUrl='<%# "http://myPath&record=" + DataBinder.Eval(Container, "DataItem.CaseID") %>' Text="Issue"></asp:HyperLink>
</ItemTemplate>
Not sure on the syntax can I do
Visible = <% iif(databinder.eval(container, "dataItem.caseid")==null, false, true) %>
Not sure how to get the syntax correct. I basically want to check if my `DataItem.CaseID is null and hide this field if it is.
I ended up using this:
Visible='<%# Eval("SugarCaseID") != DBNull.Value %>'
visible='<%# Eval("dataItem.caseid") != null) %>'
Give this a shot
You can do this
bool ShowLink(obj data)
{
if(data!=null) {return true; } return false;
}
aspx:
<asp:HyperLink ID="hlSugar" Visible='<%# ShowLink(Eval("CaseID"))%>'
ToolTip="View the issue in SugarCRM." Target="_blank" runat="server"
NavigateUrl='<%# "http://myPath&record="
+ DataBinder.Eval(Container, "DataItem.CaseID") %>' Text="Issue">
</asp:HyperLink>
Use this
<asp:HyperLink ID="hlSugar" Visible='<%# Convert.ToBoolean(Eval("DataItem.CaseID").ToString() == "0") %>' ToolTip="View the issue in SugarCRM." Target="_blank" runat="server" NavigateUrl='<%# "http://myPath&record=" + DataBinder.Eval(Container, "DataItem.CaseID") %>' Text="Issue" />
REference:
Conditions within .aspx file for ListView ItemTemplate

Categories

Resources