[英]how to get value by index and not by column name in asp:repeater #Eval
I am trying to get the data using column index and not the column name using the <%#Eval('foo')%> expression (or any other way) in my repeater control. 我试图在中继器控件中使用列索引而不是在<%#Eval('foo')%>表达式(或任何其他方式)中获取列名称。 here are my codes :
这是我的代码:
page : 页面:
<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>
code-behind: 后台代码:
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();
the problem here is that for some reason(I'd be more than happy to know why), I cannot use column names for the rows that I am binding to my repeater control.(And I double checked that they actually have the data in them).So the only solution that is in front of me is to get them by their column indexes, and I have no idea how I can do it.Any solutions? 这里的问题是由于某种原因(我很高兴知道为什么),我无法为要绑定到中继器控件的行使用列名(而且我再次检查了它们实际上是否包含数据)因此,摆在我面前的唯一解决方案是通过它们的列索引来获取它们,而我不知道如何做到这一点。
Assuming that you are binding a collection of SuperItem
objects ( SuperItem
type supports index-based access) you can handle ItemDataBound
event: 假设您绑定了
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>
Code-behind: 后台代码:
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.