简体   繁体   中英

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. 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:

<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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM