asp.net datalist data binding
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 knowl开发者_如何学Pythonedge 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.
<%# %> 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") %>
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>
<%# (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.
精彩评论