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

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?

Related

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

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") %>

Passing Container.Eval to (Html.ReaderPartial) inside ASP.NET Repeater Control

I am trying to pass Eval to Html.RenderPartial inside ASP.NET Repeater but it does not work can any one help?
<asp:Repeater runat="server">
<ItemTemplate>
<% Html.RenderPartial("UserControl1",Eval("Title")); %>
</ItemTemplate>
</asp:Repeater>
by the way I know that I can do it in other ways but I want to know if it is doable or not.
is the same as in that it expects an expression that returns a string, so to get this compiling you have to call a method that calls Html.RenderPartial(), then returns an empty string:
<%
protected string RenderControl(object dataItem)
{
Html.RenderPartial("UserControl1", ((MyType) dataItem).Title);
return "";
}
%>
... <%# RenderControl(Container.DataItem) %> ...
I would just use foreach though - mixing WebForms data-binding and MVC partial rendering is unpredictable, at best:
<% foreach (MyObject o in data) { Html.RenderPartial("UserControl1", o.Title); } %>
Don't make life any harder than it needs to be...
Try putting your RenderPartial inside <%# %> statement like:
<asp:Repeater runat="server">
<ItemTemplate>
<%# Html.RenderPartial("UserControl1",Eval("Title")); %>
</ItemTemplate>
</asp:Repeater>

Using Eval inside a IF statement and Repeater

I am trying to use Eval inside a IF Statement and Repeater.
I want to do something like this:
<asp:Repeater runat="server" ID="rpRepeater">
<ItemTemplate>
<% if ((bool)Eval("A_Boolean"))
{ %>
blah...
<% } %>
</ItemTemplate>
</asp:Repeater>
This code gives me the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Eval can be only used inside "binding" tag.
<%# Eval("A_Boolean") %>
http://support.microsoft.com/kb/307860#1a
It is possible to simulate if statements like this (code goes within ItemTemplate).
<asp:Panel runat="server" Visible='<%# Eval("A_Boolean") %>'>
blah...
</asp:Panel>

VB.NET Repeater Simple Data Binding Without Datasource

Im a ASP.NET beginner. I previously asked how to do some databinding to a repeater without a datasourse. Here. VB.NET Repeater Simple Data Binding Without Datasource
here is the solution someone got for me
Dim repeatTimes((TotalAdInsured)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
However, I now need to know how to get an index for each item in the repeater. ie
<% #index %>
<asp:Repeater runat="server" ID="rptAwesome">
<ItemTemplate>
<%# Container.DataItem %> <%# Container.ItemIndex %><br />
</ItemTemplate>
</asp:Repeater>

Resources