asp.net datalist data binding
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.
zack
Updated on April 07, 2020Comments
-
zack about 4 years
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 myDataList
tag in the markup? Also additional knowledge on how to bind aDataList
to aDataSet
,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?
-
zack over 13 yearsthanks. but for now what do I write inside the tag if lets say i HAVE to bind to a NameValueCollection 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?
-
jcolebrand over 13 years@VP ~ @Dismissile already gave you the answer there. You would
Eval('Value')
orEval('Name')
according to which you wanted. -
zack over 13 yearsthere is no DATAITEMNAME as you can see that I am binding it to a namevaluecollection. Its just a collection of string. There is no property to bind to. I have updated my question
-
jcolebrand over 13 years@VP ~ I take that back. No you can't. So instead, you should try to find a different form of storing your information, or you should convert it right before you need to consume it.
-
jcolebrand over 13 years@VP ~ My apologies. I can see that I really can't assume that you know that a
'value'
in C# means a char and for strings one should use"value"
... in other words,Acceptable
:'|', "|", "||"
~~Not Acceptable
:'||'
-
zack over 13 yearsI do understand the difference between a string inside "" and char inside ''. The link you posted above works for me. Thanks a lot.
-
jcolebrand over 13 years@VP ~ lol, I figured you did ;) ... Or at least I hoped you did. I just find myself shortcutting that in examples. Maybe I shouldn't. Just looks cleaner to use a single quote. Glad the link helped.
-
Nathan Tuggy about 9 yearsGenerally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.)