Asp Repeater Newline in constant error - asp.net

Hi I'm trying to use a conditional state inside <asp:Repeater> but I'm getting a Newline in constant error. The error is inside the <ItemTemplate>
Code:
<asp:Repeater runat="server" DataSource='<%# Eval("Slides") %>'>
<ItemTemplate>
<%# Eval("SlideId") == "one" ? "<span class='slide-option selected' id='slide-option-<%# Eval("SlideId")%>'></span>" : "<span class='slide-option' id='slide-option-<%# Eval("SlideId")%>'></span>" %>
</ItemTemplate>
</asp:Repeater>
Maybe I'm blind but I don't see any missing character. Or Is there a better way of using a conditional statement in this?

You can try this:
<%# string.Format("<span class='slide-option{0}' id='slide-option-{1}'></span>", Eval("SlideId").ToString() == "one" ? " selected" : "", Eval("SlideId")) %>

Related

Binding a value in asp.net

I want to bind the non zero value in the text field.
I have written like this :
<asp:TextBox ID="txtHaulZoneCodeLEM" runat="server" CssClass="cagText" Text='<%# Bind("HaulZoneCodeLEM") %>'></asp:TextBox
How to make sure the value is non-Zero in this field?. I don't want Zero in this text field.
I am tried like this :its giving syntax error : "Identifier expected"
<asp:TextBox ID="txtHaulZoneCodeLEM" runat="server" CssClass="cagText" Text='<%# Eval("HaulZoneCodeLEM") != 0 ? Eval("HaulZoneCodeLEM") : "" %>'></asp:TextBox>
try this
<%# Eval("HaulZoneCodeLEM").ToString().Equals("0") ? ""
:Eval("HaulZoneCodeLEM").ToString() %> hope this will help you

Add div conditionally says The name container does not exist in the current context

I want to add HTML div conditionally in rotator control. I am using the following code but it says "The name container does not exist in the current context" at DataBinder.Eval(Container.DataItem, "COL_ID")
<telerik:RadRotator ID="rtrList" runat="server" Width="830px" Height="100px"
FrameDuration="10" RotatorType="ButtonsOver" ScrollDuration="450" WrapFrames="true"
ItemWidth="100" ItemHeight="80" >
<ItemTemplate>
<asp:HiddenField ID="hdfId" runat="server" Value='<%# Eval("COL_ID") %>' />
<div id="div" runat="server" title='<%# Eval("NAME") %>' class="widget_item">
<span>
<%# Eval("TITLE") %>
</span>
</div>
<%if (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COL_ID")) % 2 == 0)
{%>
<div></div>
<%} %>
</ItemTemplate>
</telerik:RadRotator>
Please guide me where I am wrong. as I am using DataBinder.Eval & Container.DataItem for the first time.
Regards,
Kash
The error implies that this data item does not exist in the item bound to the control.
For example. If you were binding to a table, the table must have a column called "Col_ID" or this will throw an error.
Can you add your code for binding the control to the data source?
Use this syntax instead of the if:
<%# (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COL_ID")) % 2 == 0) ? "<div></div>" : "" %>

using an if/then in a repeater in an asp.net page

I haven't used asp.net webforms in what seems like forever, and I am having the toughest time figuring out how to get a simple if/then statement to work. This is what I want to do:
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<% if(Eval("show")) { %> show something <% } %>
</ItemTemplate>
</asp:repeater>
But obviously that gives me an error - how do I do this? Thank you - I have completely gone to MVC now and I can't seem to remember this stuff.
You are missing an ItemTemplate
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<%# Eval("show") ? Eval("Whatever") : Eval("Whatever") %>
</ItemTemplate>
</asp:repeater>
Try the conditional operator
<%= Convert.ToBoolean(Eval("show")) ? "something" : "" %>
If you just want to show a simple string or something, you can do this:
<%# bool.Parse(Eval("show")) ? "show something" : null %>
What exactly are you trying to show/not show?

Using '<%# Eval("item") %>'; Handling Null Value and showing 0 against

If dataitem is Null I want to show 0
<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label>
How can I accomplish this?
You can also create a public method on the page then call that from the code-in-front.
e.g. if using C#:
public string ProcessMyDataItem(object myValue)
{
if (myValue == null)
{
return "0 value";
}
return myValue.ToString();
}
Then the label in the code-in-front will be something like:
<asp:Label ID="Label18" Text='<%# ProcessMyDataItem(Eval("item")) %>' runat="server"></asp:Label>
Sorry, haven't tested this code so can't guarantee I got the syntax of "<%# ProcessMyDataItem(Eval("item")) %>" entirely correct.
I'm using this for string values:
<%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%>
You can also use following for nullable values:
<%#(Eval("Data") == null ? "0" : Eval("Data"))%>
Also if you're using .net 4.5 and above I suggest you use strongly typed data binding:
<asp:Repeater runat="server" DataSourceID="odsUsers" ItemType="Entity.User">
<ItemTemplate>
<%# Item.Title %>
</ItemTemplate>
</asp:Repeater>
I use the following for VB.Net:
<%# If(Eval("item").ToString() Is DBNull.Value, "0 value", Eval("item")) %>
It should work as well
Eval("item") == null?"0": Eval("item");
Moreover, you can use (x = Eval("item") ?? 0) in this case.
http://msdn.microsoft.com/en-us/library/ms173224.aspx
I don't know ASP.NET very well, but can you use the ternary operator?
http://en.wikipedia.org/wiki/Ternary_operation
Something like:
(x=Eval("item")) == Null ? 0 : x
Use IIF.
<asp:Label ID="Label18" Text='<%# IIF(Eval("item") Is DBNull.Value,"0", Eval("item") %>'
runat="server"></asp:Label>
try this code it might be useful -
<%# ((DataBinder.Eval(Container.DataItem,"ImageFilename").ToString()=="") ? "" :"<a
href="+DataBinder.Eval(Container.DataItem, "link")+"><img
src='/Images/Products/"+DataBinder.Eval(Container.DataItem,
"ImageFilename")+"' border='0' /></a>")%>
Used a modified version of Jason's answer:
public string ProcessMyDataItem(object myValue)
{
if (myValue.ToString().Length < 1)
{
return "0 value";
}
return myValue.ToString();
}
Try replacing <%# Eval("item") %> with <%# If(Eval("item"), "0 value") %> (or <%# Eval("item") ?? "0 value" %>, when using C#).
I have tried this code and it works well for both null and empty situations :
'<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'
You can use the following for VB.net, especially if the value is a boolean:
<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>
For example, use this to automatically check or uncheck a checkbox with inline IIF eval:
<asp:CheckBox ID="mycheckbox" runat="server" Checked='<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>' />
The other method with .ToString will cause an error trying to convert the DBnull to a boolean.

ASP.NET FormView: Including a Member Variable in an XPath Expression

Is something like this possible?
<asp:FormView ID="myFormView" runat="server">
<ItemTemplate>
<p><%# XPath("Root/Path/Item[#id=<%=this.m_myId%>]/HelloWorld")%></p>
</ItemTemplate>
</asp:FormView>
m_myId is a member variable on the page. I can't seem to get it to work without a parsing error.
You can't have nested asp tags. Try something like this:
<%# XPath("Root/Path/Item[#id= " + this.m_myId + "]/HelloWorld") %>

Resources