我正在使用 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
绑定到 DataSet
、sqldatareader
、IList<>
的其他知识也会有所帮助。 。
谢谢大家。但现在如果我必须绑定到 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?
发布评论
评论(4)
我在我的代码隐藏中声明了我的
List
(比如......在附加到 OnClick 的方法中),然后我将像这样进行数据绑定:现在请了解我的代码是多么简化,我没有进行任何错误检查(例如,如果数据库丢失或者您没有返回结果),并且我没有定义参数或 ComplexObject(因为我假设您了解这些东西是如何工作的)。
在页面的 .aspx 中,我将在
DataList
控制字段的ItemTemplate
中定义,其中<%# Eval('ComplexObjectFieldOneName') % >
或<%# Eval('ComplexObjectFieldTwoName') %>
(等等)。因此,给定 a,
我会将 .aspx 中的字段定义为
<%# Eval('MyFirstField') %>
和<%# Eval('MySecondField') %>
好吧,这有点啰嗦,所以我希望它确实有帮助。
另一点:您还可以使用 ObjectDataSources(或 SqlDataSource 等派生类)并在 .aspx 上进行所有链接,假设正确构建了对象类。需要考虑的事情。
I declare my
List<ComplexObject>
in my codebehind (say ... in a method attached to an OnClick) and then I will databind it like so: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 theDataList
control fields where I<%# Eval('ComplexObjectFieldOneName') %>
or<%# Eval('ComplexObjectFieldTwoName') %>
(etc).So given a
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.
<%#%>是数据绑定语法。
您通常会执行以下操作:
这定义了到数据源中名为 PropertyName 的属性/列的单向绑定。
就您而言,我认为您可以执行“名称”或“值”,因为这些是 NameValueCollection 的公共属性。
您还可以使用以下方法进行双向数据绑定:
<%# %> is the databinding syntax.
You usually do something like:
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:
将数据列表源绑定到数据视图或数据表。
数据列表.Datasource = DataView;
Bind the datalist source to the data view or datatable.
Datalist.Datasource = DataView;
数据源是您要绑定到的实际集合,但我们必须对其进行转换。当然,Container.DataItem 是 KEY,我将其转换为字符串以便可以使用。
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.