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