![](/img/trans.png)
[英]HTML Decoding of Eval database column value in repeater of ASP.NET webforms
[英]how to get value by index and not by column name in asp:repeater #Eval
我试图在中继器控件中使用列索引而不是在<%#Eval('foo')%>表达式(或任何其他方式)中获取列名称。 这是我的代码:
页面:
<asp:Repeater ID="rptrHeatingNavien" runat="server">
<ItemTemplate>
<li class="icon-font"><a href="/Products/?Id=<%#Eval('get-data-by-index[0]')%>><a><%#Eval('get-data-by-index[1]')%></a></li>
</ItemTemplate>
</asp:Repeater>
后台代码:
string connectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(connectionString);
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Products", sqlCon);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
rptr.DataSource = dsSideMenu.Tables[0].Select("category = '1-1-1'");
rptr.DataBind();
这里的问题是由于某种原因(我很高兴知道为什么),我无法为要绑定到中继器控件的行使用列名(而且我再次检查了它们实际上是否包含数据)因此,摆在我面前的唯一解决方案是通过它们的列索引来获取它们,而我不知道如何做到这一点。
假设您绑定了SuperItem
对象的集合( SuperItem
类型支持基于索引的访问),则可以处理ItemDataBound
事件:
<asp:Repeater ID="rptrHeatingNavien" runat="server" OnItemDataBound="rptrHeatingNavien_ItemDataBound">
<ItemTemplate>
<li class="icon-font">
<asp:HyperLink runat="server" ID="productLink" />
</ItemTemplate>
</asp:Repeater>
后台代码:
protected void rptrHeatingNavien_ItemDataBound(object sender, EventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var item = (SuperItem) e.Item.DataItem;
var link = (HyperLink) e.Item.FindControl("productLink");
link.NavigateUrl = string.Format("/Products/?Id={0}", item[0].ToString());
link.Text = item[1].ToString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.