0

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?

roostaamir
  • 1,928
  • 5
  • 24
  • 51

1 Answers1

0

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();
    }
}
nativehr
  • 1,131
  • 6
  • 16