asp.net datalist 数据绑定

发布于 2024-09-28 19:19:23 字数 997 浏览 4 评论 0 原文

我正在使用 ASP.NET/C# DataList

 <asp:DataList ID="EquipmentList" RepeatColumns="5".....  

我在 > 中有以下行:标签:

 <a href=""`><%# {I want to put something here but dont know how} %> </a>  

在我的代码后面,我有一个包含所有字符串的 NameValueCollection 变量:

 NameValueCollection myListofStrings = //calling a method here that populates myListofStrings   
this.EquipmentList.DataSource =  myListofStrings;  
this.EquipmentList.DataBind();

请有人告诉我如何将此 NameValueCollection 变量绑定到我的 DataList 标记中的标记?此外,有关如何将 DataList 绑定到 DataSetsqldatareaderIList<> 的其他知识也会有所帮助。 。

谢谢大家。但现在如果我必须绑定到 1NameValueCollection1 变量(就像上面的例子一样),我应该在标签内写什么。它没有属性或列,因此我无法编写类似 Eval("propertyname") 的内容,这是这里大多数人给我的答案。就像我将它绑定到一个字符串数组一样。

那我现在写什么呢?

I am using an ASP.NET/C# DataList.

 <asp:DataList ID="EquipmentList" RepeatColumns="5".....  

I have the following line inside the <ItemTemplate> tag:

 <a href=""`><%# {I want to put something here but dont know how} %> </a>  

In my code behind I have a NameValueCollection variable that contains all strings:

 NameValueCollection myListofStrings = //calling a method here that populates myListofStrings   
this.EquipmentList.DataSource =  myListofStrings;  
this.EquipmentList.DataBind();

Please can someone tell me how to bind this NameValueCollection variable to my DataList tag in the markup? Also additional knowledge on how to bind a DataList to a DataSet, sqldatareader, IList<> would be helpful.

Thank you all. but for now what do I write inside the tag if lets say I have to bind to a 1NameValueCollection1 variable like in my case above. It has no properties or columns so I cannot write anything like Eval("propertyname") which is the answer that most here gave me. It is just like I am binding it to an array of strings.

So what do I write now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪装你 2024-10-05 19:19:23

请有人告诉我如何绑定
此 NameValueCollection 变量为
我的数据列表标签在标记中?还
有关如何绑定的附加知识
datalist 到数据集,sqldatareader,
IList<>会有帮助的。谢谢

我在我的代码隐藏中声明了我的 List (比如......在附加到 OnClick 的方法中),然后我将像这样进行数据绑定:

private void DoDataGetAndBind() {
  List<ComplexObject> complexObjects = _dataAccessLayer.GetComplexObjectsMethod(parameter1, parameter2, sortParameter);
  datalist1.DataSource = complexObjects;
  datalist1.DataBind();
}

现在请了解我的代码是多么简化,我没有进行任何错误检查(例如,如果数据库丢失或者您没有返回结果),并且我没有定义参数或 ComplexObject(因为我假设您了解这些东西是如何工作的)。

在页面的 .aspx 中,我将在 DataList 控制字段的 ItemTemplate 中定义,其中 <%# Eval('ComplexObjectFieldOneName') % ><%# Eval('ComplexObjectFieldTwoName') %> (等等)。

因此,给定 a,

public class ComplexObject {
  public string MyFirstField {get;set;}
  public string MySecondField {get;set;}
}

我会将 .aspx 中的字段定义为 <%# Eval('MyFirstField') %><%# Eval('MySecondField') %>

好吧,这有点啰嗦,所以我希望它确实有帮助。


另一点:您还可以使用 ObjectDataSources(或 SqlDataSource 等派生类)并在 .aspx 上进行所有链接,假设正确构建了对象类。需要考虑的事情。

Please can someone tell me how to bind
this NameValueCollection variable to
my datalist tag in the markup? Also
additional knowledge on how to bind a
datalist to a dataset, sqldatareader,
IList<> would be helpful. Thannks

I declare my List<ComplexObject> in my codebehind (say ... in a method attached to an OnClick) and then I will databind it like so:

private void DoDataGetAndBind() {
  List<ComplexObject> complexObjects = _dataAccessLayer.GetComplexObjectsMethod(parameter1, parameter2, sortParameter);
  datalist1.DataSource = complexObjects;
  datalist1.DataBind();
}

Now please understand how simplified my code is, I didn't put any error checks (like, if the database dropped or you returned no results) and I didn't define the parameters or the ComplexObject (because I presume you understand how those things work).

In the .aspx of the page, I would then define inside the ItemTemplate of the DataList control fields where I <%# Eval('ComplexObjectFieldOneName') %> or <%# Eval('ComplexObjectFieldTwoName') %> (etc).

So given a

public class ComplexObject {
  public string MyFirstField {get;set;}
  public string MySecondField {get;set;}
}

I would define the fields in the .aspx as <%# Eval('MyFirstField') %> and <%# Eval('MySecondField') %>

Ok, that was rather long winded, so I hope it really did help.


Another point: You can also use ObjectDataSources (or the derived classes like SqlDataSource, etc) and do all the linking on the .aspx, assuming properly built object classes. Something to consider.

花桑 2024-10-05 19:19:23

<%#%>是数据绑定语法。

您通常会执行以下操作:

<%# Eval("PropertyName") %>

这定义了到数据源中名为 PropertyName 的属性/列的单向绑定。

就您而言,我认为您可以执行“名称”或“值”,因为这些是 NameValueCollection 的公共属性。

您还可以使用以下方法进行双向数据绑定:

<%# Bind("PropertyName") %>

<%# %> is the databinding syntax.

You usually do something like:

<%# Eval("PropertyName") %>

This defines a one-way binding to a property/column named PropertyName in your data source.

In your case I think you can do either Name or Value since those are the public properties of NameValueCollection.

You can also do a two way data binding using:

<%# Bind("PropertyName") %>
大姐,你呐 2024-10-05 19:19:23

将数据列表源绑定到数据视图或数据表。

数据列表.Datasource = DataView;

   <ITEMTEMPLATE>
    <ASP:LABEL id="lblField" runat="server" Font-Bold="true">
    <%# DataBinder.Eval(Container.DataItem, "DATAITEMNAME") %>
    </ASP:LABEL>
    </ITEMTEMPLATE>

Bind the datalist source to the data view or datatable.

Datalist.Datasource = DataView;

   <ITEMTEMPLATE>
    <ASP:LABEL id="lblField" runat="server" Font-Bold="true">
    <%# DataBinder.Eval(Container.DataItem, "DATAITEMNAME") %>
    </ASP:LABEL>
    </ITEMTEMPLATE>
稳稳的幸福 2024-10-05 19:19:23
<%# (EquipmentList.DataSource as NameValueCollection)[Container.DataItem as string] %>

数据源是您要绑定到的实际集合,但我们必须对其进行转换。当然,Container.DataItem 是 KEY,我将其转换为字符串以便可以使用。

<%# (EquipmentList.DataSource as NameValueCollection)[Container.DataItem as string] %>

The datasource is the actually collection you are binding to, but we have to convert it. And of course the Container.DataItem is the KEY, in which I convert to a string so it can be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文