views:

26

answers:

3

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

thank you all. 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?

A: 

<%# %> 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") %>
Dismissile
'Name' error - Too many characters in character literal.
VP
+1  A: 

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.

drachenstern
thanks. 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?
VP
@VP ~ @Dismissile already gave you the answer there. You would `Eval('Value')` or `Eval('Name')` according to which you wanted.
drachenstern
@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.
drachenstern
@VP ~ See this link http://forums.asp.net/t/1016703.aspx
drachenstern
'Name' error - Too many characters in character literal.
VP
@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` : `'||'`
drachenstern
I do understand the difference between a string inside "" and char inside ''. The link you posted above works for me. Thanks a lot.
VP
@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.
drachenstern
A: 

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>
Kirit Chandran
there 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
VP