Solved

can I bind my arralist to a datalist?

Posted on 2007-11-29
11
754 Views
Last Modified: 2012-05-05
hi there,
Im trying to output an arraylist using a datalist.  I have tried the following although this fails..

    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "_options")%>
    </ItemTemplate>

The complication I have that I cant get around is that the arraylist is a property of my custom built class.  Therefore I have the following structure (included within the code snippet part) which I bind to using the following

        DataList1.DataSource = results._options;
        DataList1.DataBind();

Problem I have is that I dont know what to put within the markup, denoted below with the triple x to output the elements within the arraylist!

<li><%# DataBinder.Eval(Container.DataItem, "xxx") %></li>

Please tell me how!

With this working I expect to get the following html

<li>Terry</li>
<li>Jane</li>
<li>Tim</li>

If I was to do the following

_options.add("terry");
_options.add("Jane");
_options.add("Tim");


Thanks


public class results

{

    public string _title;

    public ArrayList _options = new ArrayList();

 

	public results()

	{

	}

}

Open in new window

0
Comment
Question by:jimbona27
  • 8
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
Roopesh_7 earned 350 total points
ID: 20375585
do like this

class myClass
{
public string _name;

public myClass(string name)
{
_name = name;
}

public string Name
{
get
{
return _name;
}
}

<aspataList id="DataList1" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Name") %>
</ItemTemplate>
</aspataList>

public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList al = new ArrayList();
al.Add(new myClass("Peter"));
al.Add(new myClass("Mary"));

DataList1.DataSource = al;
DataList1.DataBind();
}
}


0
 

Author Comment

by:jimbona27
ID: 20388153
yeah great, this is pretty much what i've been doing, creating objects which is always a good thing anyway...

although what your doing different to me is

al.Add(new myClass("Peter"));

whereas i've used datalists with a datatable behind it to get around it, not knowing you can do what you've done here.
i'll try it on monday to know for sure whether it works for me.
thanks in the meantime.

0
 

Author Comment

by:jimbona27
ID: 20396491
i've changed my code now but how do I refer to the property within the markup, like so

<%# DataBinder.Eval(Container.DataItem, "xxx") %>

I've tried


<%# DataBinder.Eval(Container.DataItem, "Name") %>

although this fails:
 Exception Details: System.Web.HttpException: DataBinding: 'classname' does not contain a property with the name 'Name'.

Note: The Name is a public variable within the object like below:




public class test

{

    public string _Name;

    ....
 

}

Open in new window

0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 20396567
could you please sent whole code in cs. B'cos its working here
0
 

Author Comment

by:jimbona27
ID: 20396593
i've seen this online but fails for me:

<%#  ((System.Collection.ArrayList)DataBinder.Eval(Container.DataItem, "[\"myarraylist\"]") )[0] %>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jimbona27
ID: 20402003
does this help?
the error message I get:

 DataBinding: 'test does not contain a property with the name '_username'.

thanks
// code that creates the arraylist
 

        ArrayList options = new ArrayList();
 

        while (reader.Read())

        {

            options.Add(new test((Int32)reader["id"],(string)reader["username"],(string)reader["address"]);

        }
 

...
 

// the bind
 

        datalist1.DataSource = db.GetData();

        datalist1.DataBind();
 
 

// markup
 

<%# DataBinder.Eval(Container.DataItem, "Username") %>
 
 
 

// the class
 
 

public test

{

    public Int32 _id;

    public string _a;

    public string _b;

    public string _c;
 

    public comment()

    {

    }

	public comment(Int32 a, string b, string c)

	{

        _a = a;

        _b = b;

        _c = c;

	}
 

}

Open in new window

0
 

Author Comment

by:jimbona27
ID: 20402005
sorry markup should read

<%# DataBinder.Eval(Container.DataItem, "_username") %>
 
0
 

Author Comment

by:jimbona27
ID: 20402313
think i got it.
you have to have a method to return the property of the object to use in the markup.
i was just using the public property itself.
0
 

Author Comment

by:jimbona27
ID: 20402404
how do i get access to the properties in the code behind file?

so i have an arraylist of cars

i.e.
        cars test = new cars();
        test = (cars)myfunctiion();  // cast the return arraylist into car objects -- fails

i can kinda see why but I dont know how to do it.

i want to access the 'id' property within my object,

something probably along this line although it fails for me:

response.write(myarraylist.row[0].column[1]);

writes "hello"


// creating the arraylist with objects
 

public arraylist myfunctiion()

{

        ArrayList test = new ArrayList();
 

        while (reader.Read())

        {

            

            test.Add(new cars("10","hello");

        }
 

        return test;

}

Open in new window

0
 

Author Comment

by:jimbona27
ID: 20402448
got it.
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 20403189
Good...I just wait for you...:) you are on the right track
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio: built-in keystroke automation 2 46
SSRS ReportViewer report timeout 7 100
Error in page 3 45
When should I use a controller? 3 0
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now