asp.net Web 表单:从动态生成的列表中选择项目以在代码隐藏中使用
asp.net web forms: pick item from dynamically generated list to use in codebehind
<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>
显然我是 Webforms 的新手。是否可以向 foodReports 显示的每个值添加链接按钮,并根据选择的按钮(通过索引或其他方式)执行不同的操作?
根据我看过的其他答案,我尝试使用 CommandArgument = "<% =count %>"
添加,但 CommandArgument 的字面意思是“<% =count %>”,而不是获取特定索引。任何允许我保留每个列出项目的样式的解决方案都可以。
"I have tried adding with CommandArgument = "<% =count %>" but the CommandArgument literally just comes out as "<% =count %>" instead of getting specific indexes."
第一
您不是 WebForms 的新手,您只知道 "other answers I've looked at".
所以买一些初学者的书,在 1 周内你会看到你的问题,并会因为让你的祖先难堪而牺牲你的生命来荣耀撒旦。
开玩笑...不是...也许...好吧,回答
回答
使用模型绑定技术
这里我会用Repeater
和ObjectDataSource
给大家演示一下。
数据访问对象 (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;
}
}
}
型号
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; }
}
}
设计器视图
<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>
代码隐藏
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";
}
}
<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>
显然我是 Webforms 的新手。是否可以向 foodReports 显示的每个值添加链接按钮,并根据选择的按钮(通过索引或其他方式)执行不同的操作?
根据我看过的其他答案,我尝试使用 CommandArgument = "<% =count %>"
添加,但 CommandArgument 的字面意思是“<% =count %>”,而不是获取特定索引。任何允许我保留每个列出项目的样式的解决方案都可以。
"I have tried adding with CommandArgument = "<% =count %>" but the CommandArgument literally just comes out as "<% =count %>" instead of getting specific indexes."
第一
您不是 WebForms 的新手,您只知道 "other answers I've looked at".
所以买一些初学者的书,在 1 周内你会看到你的问题,并会因为让你的祖先难堪而牺牲你的生命来荣耀撒旦。
开玩笑...不是...也许...好吧,回答
回答
使用模型绑定技术
这里我会用Repeater
和ObjectDataSource
给大家演示一下。
数据访问对象 (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;
}
}
}
型号
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; }
}
}
设计器视图
<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>
代码隐藏
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";
}
}