简体   繁体   English

如何在Asp:repeater #Eval中按索引而不是按列名获取值

[英]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.

相关问题 ASP.NET Webforms的中继器中Eval数据库列值的HTML解码 - HTML Decoding of Eval database column value in repeater of ASP.NET webforms C#ASP.NET如何从Code-Behind插入Eval值重复器 - C# ASP.NET How to insert Eval value Repeater from Code-Behind 如何在asp.net中的中继器控件中计算最后一列的值 - how to count value of last column in repeater control in asp.net 在Repeater ASP.NET控件中以所需样式动态显示列名称和列值 - Display column name and column value dynamically in desired style in Repeater ASP.NET control 如何使用Eval()引用ASP中继器中SortedDictionary中的值? - How do I use Eval() to reference values in a SortedDictionary in an asp Repeater? 如何在repeater_itemdatabound事件中获得一列值? - how to get one column value in repeater_itemdatabound event? 使用列的索引或 C# ASP .NET 中的列名来访问 DataSet 中单元格的值有多值得推荐? - How recommendable is it to access the value of a cell in a DataSet using the index of the column or the name of the column in C# ASP .NET? 如何通过列名获取列索引? - how to get column Index by column name? 使用方法在asp:repeater中获取价值 - Use method to get value in asp:repeater asp net repeater单选按钮组名称和获取单选按钮值 - asp net repeater radio button group name and get radio button value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM