如何通过索引获取值,而不是通过asp:repeater #Eval中的列名获取值

时间:2015-01-04 20:23:50

标签: c# asp.net repeater datarow datatable.select

我正在尝试使用列索引而不是列名使用<%#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();

这里的问题是由于某种原因(我非常乐意知道原因),我不能将列名用于我绑定到我的转发器控件的行。(我仔细检查了它们实际上有数据在其中)。所以我面前的唯一解决方案是通过他们的列索引获取它们,我不知道我该怎么做。任何解决方案?

1 个答案:

答案 0 :(得分:0)

假设您绑定了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();
    }
}