Reputation: 27
<div>Found <%=foodCount%> results</div>
<div class="row">
<%if (foodReports != null)
{ %>
<%int count = 0; %>
<% foreach (var f in foodReports)
{
if (f.NRF63 > 28)
style = "green";
if (f.NRF63 <= 28 && f.NRF63 >= 4.66)
style = "#fabb36";
if (f.NRF63 < 4.66)
style = "#c32032";
%>
<div class="col-xs-12 mar-bottom-xs">
<ul class="list-unstyled price-holder md-round bg-light pad-bottom-xs">
<li class="price-heading" style="background: <%=style%>;">
<div class="align-left pull-left">
<strong><%=f.desc.name%></strong>
<span class="txt"></span>
</div>
<div class="align-right pull-right">
<strong class="Price">Nutrition Index: <%=f.NRF63%></strong>
<span class="txt">P/USDA</span>
</div>
</li>
</ul>
</div>
<%count++; %>
<% }
} %>
</div>
Obviously I am new to Webforms. Is it at all possible to add linkbuttons to each value being displayed from foodReports and perform different actions based on which button was selected (by the index or whatever?)
Based on other answers I've looked at, I have tried adding with CommandArgument = "<% =count %>"
but the CommandArgument literally just comes out as "<% =count %>" instead of getting specific indexes. Any solution that allows me to keep the styling of each listed item will work.
Upvotes: 0
Views: 303
Reputation: 1293
"I have tried adding with CommandArgument = "<% =count %>" but the CommandArgument literally just comes out as "<% =count %>" instead of getting specific indexes."
First
You're not NEW in WebForms, you know only the "other answers I've looked at".
So buy some book for beginners and in 1 week you will look at your question and will offer your life in sacrifice to the glory of Satan for having embarrassed your ancestors.
Joking... on not... maybe... ok, to the answer
Answer
Use model binding tecnology
Here i will demonstrate to you using Repeater
and ObjectDataSource
.
Data Access Object (DAO)
using MyWebApplication.Models;
namespace MyWebApplication.DAO
{
public class FoodDAO
{
public List<Food> GetFoodList()
{
var foodList = new List<Food>();
foodList.Add(new Food
{
Index = 1,
NRF63 = 13.4m,
Desc = new Desc { Name = "Potato" }
});
foodList.Add(new Food
{
Index = 2,
NRF63 = 2.15m,
Desc = new Desc { Name = "McDonalds" }
});
foodList.Add(new Food
{
Index = 3,
NRF63 = 8000,
Desc = new Desc { Name = "Bacon" }
});
return foodList;
}
}
}
Model
namespace MyWebApplication.Models
{
public class Food
{
public decimal NRF63 { get; set; }
public Desc Desc { get; set; }
public int Index { get; set; }
public string Style
{
get
{
if (NRF63 > 28)
{
return "green";
}
else if (NRF63 <= 28 && NRF63 >= 4.66m)
{
return "#fabb36";
}
else
{
return "#c32032";
}
}
}
}
public class Desc
{
public string Name { get; set; }
}
}
Designer View
<asp:Repeater ID="FoodRepeater" runat="server"
DataSourceID="FoodDataSource"
ItemType="MyWebApplication.Models.Food">
<HeaderTemplate>
<div class="row">
</HeaderTemplate>
<ItemTemplate>
<div class="col-xs-12 mar-bottom-xs">
<ul class="list-unstyled price-holder md-round bg-light pad-bottom-xs">
<li class="price-heading" style='<%# "background:" + Item.Style + ";" %>'>
<div class="align-left pull-left">
<strong><%# Item.Desc.Name %></strong>
<span class="txt"></span>
</div>
<div class="align-right pull-right">
<strong class="Price">Nutrition Index: <%# Item.NRF63.ToString("{0:0.00}") %></strong>
<span class="txt">P/USDA</span>
</div>
</li>
<li>
<asp:LinkButton ID="SomeLinkButton" runat="server"
CommandArgument='<%# Item.Index %>'
OnClick="SomeLinkButton_Click">
<%# Item.Index %>
</asp:LinkButton>
</li>
</ul>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
<div class="row">
<asp:Label ID="lblFoodsCount" runat="server"></asp:Label>
</div>
<asp:ObjectDataSource ID="FoodDataSource" runat="server"
TypeName="MyWebApplication.DAO.FoodDAO"
DataObjectTypeName="MyWebApplication.Models.Food"
EnablePaging="false"
SelectMethod="GetFoodList"
OnSelected="FoodDataSource_Selected">
</asp:ObjectDataSource>
Code Behind
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SomeLinkButton_Click(object sender, EventArgs e)
{
var button = sender as LinkButton;
int index;
if (!int.TryParse(button.CommandArgument, out index))
return;
// Do something with 'index'
}
protected void FoodDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
lblFoodsCount.Text = "The server return " +
((IList<Food>)e.ReturnValue).Count + " foods";
}
}
Upvotes: 1