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

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

Related

How to get current row textbox value on button click but NOT in code-behind

Hi I have the following TextBox:
<asp:TemplateField HeaderText="Email">
<EditItemTemplate>
<asp:TextBox ID="txt_email" runat="server" Text='<%#Eval("usr_email") %>' class="custom-table-text"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
If I need to get Eval\db value I can simply do either:
<asp:LinkButton.... OnClientClick='<%# "ValidateEmail(\"" + Eval("usr_email") + "\"); return false;" %>'
<asp:LinkButton.... OnClientClick='<%# "ValidateEmail(\"" + DataBinder.Eval(Container.DataItem, "usr_email") + "\"); return false;" %>'
But how to get the value of current textbox (current row):
inside LinkButton OnClientClick.
before postback (client-side\without postback\not in code-behind).
I found it in case anyone finds it useful or has any comment:
OnClientClick='<%# String.Format("return ValidateEmail({0}.value)", (((GridViewRow)Container).FindControl("txt_email") as TextBox).ClientID) %>'

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 to conditionally generate html in ASP template

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

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

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