DropDownList Enumerate DataSource

A

AG

I have a dropdownlist bound to an objectdatasource.
I need to loop through all the items in the datasource to get the values of
a column that is not used as the datatextfield or the datavaluefield.
I thought maybe in the databound event of the dropdownlist, but don't know
how to reference the source items.

What would be the easiest way to do this?

TIA
 
J

JT

I think I understand what you mean...let me know if this is what you
are looking for

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BuildItems()
End If
End Sub

Sub BuildItems()
Try
lstBuild.Items.Clear()
Dim cmdGetSections As New OleDbCommand("SQL GOES HERE",
yourconnection)
If yourconnection.State = yourconnection.Closed Then
yourconnection.Open()
End If
Dim readSections As OleDbDataReader =
cmdGetSections.ExecuteReader()
Do While readSections.Read()

'The process that I have below will loop through the table and grab
the items you want. During this LOOP you can then evaluate a Different
Column by specifying the a different Ordinal...I will try my best to
show this below
'Lets say Column 3 has the values you want in the DropDown, and Column
4 is the Column you want to evaluate

Dim NewItem As New ListItem
If readSections.GetString(4) = "Yes" Then
NewItem.Text = readSections.GetString(3)
NewItem.Value = readSections.GetValue(3)
lstBuild.Items.Add(NewItem)
End If

Loop
Catch ex As Exception
Response.Write(ex.Message)
Finally
yourconnection.Close()
End Try
End Sub

Hope this helps...
 
A

AG

Thanks JT.
I guess I was not clear enough. The dropdownlist is bound to an object
datasource.
Yes, I can instantiate a new object that is the source for the
objectdatasource, but I thought there might be a simpler way.
Maybe something like an items collection of the datasource (not the items
collection) for the dropdownlist.
 
E

Eliyahu Goldin

Declarative databinding with xxxdatasource objects is good for only most
trivial scenarios. If you want anything beyond that, use old good
databinding of normal data access controls like datareader, dataset etc. to
the DataSource property.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
A

AG

Interesting point. Thanks!

--

AG
Email: discuss at adhdata dot com



Eliyahu Goldin said:
Declarative databinding with xxxdatasource objects is good for only most
trivial scenarios. If you want anything beyond that, use old good
databinding of normal data access controls like datareader, dataset etc.
to the DataSource property.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


AG said:
Thanks JT.
I guess I was not clear enough. The dropdownlist is bound to an object
datasource.
Yes, I can instantiate a new object that is the source for the
objectdatasource, but I thought there might be a simpler way.
Maybe something like an items collection of the datasource (not the items
collection) for the dropdownlist.
 
S

Steven Cheng[MSFT]

Hello AG,

For the DropDownList control, so far it hasn't provided a event like the
"ItemDataBound" event of DataGrid(or "RowDataBound" event of GridView
control). Therefore, it may be difficult to intercept the dataitem of
every listItem in DropDownList. However, as you're using the
ObjectDataSource, you can still have a look at the
ObjectDataSource.Selected event. In this event, the selected data(will be
bound to the target control) has been returned and you can access it
through this event's eventargument. e.g.

====================
protected void ObjectDataSource1_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
Response.Write("<br/>ReturnValue: " + e.ReturnValue);
}
=======================

so if your object datasource is returning a typed DataTable, the
"ReturnValue" above is just the typed datatable instance.

In addition, the ObjectDatasource also provides "ObjectCreated" event which
can help us get the reference to the underlying data access class
instance(configured in ObjectDataSource's TypeName). If you have some
certain methods on the class that can help do some custom functions, you
can get the referene and peform teh method call on it.

#ObjectDataSource.ObjectCreated Event
http://msdn2.microsoft.com/en-au/library/system.web.ui.webcontrols.objectdat
asource.objectcreated.aspx

Hope this also helps. If you have any further questions or any particular
requirement in your scenario, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top