473,498 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList Enumerate DataSource

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

AG
Email: discuss at adhdata dot com


Feb 17 '07 #1
6 1855
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...

Feb 17 '07 #2
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.

--

AG
Email: discuss at adhdata dot com

"JT" <ja**********@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
>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...

Feb 18 '07 #3
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" <NO**********@newsgroups.nospamwrote in message
news:%2***************@TK2MSFTNGP04.phx.gbl...
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.

--

AG
Email: discuss at adhdata dot com

"JT" <ja**********@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
>>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...


Feb 18 '07 #4
AG
Interesting point. Thanks!

--

AG
Email: discuss at adhdata dot com

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:uv**************@TK2MSFTNGP06.phx.gbl...
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" <NO**********@newsgroups.nospamwrote in message
news:%2***************@TK2MSFTNGP04.phx.gbl...
>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.

--

AG
Email: discuss at adhdata dot com

"JT" <ja**********@gmail.comwrote in message
news:11**********************@k78g2000cwa.googleg roups.com...
>>>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...



Feb 18 '07 #5
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/lib...rols.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.
Feb 19 '07 #6
AG
Thanks Steven,

That is what I was looking for.

--

AG
Email: discuss at adhdata dot com

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:nG**************@TK2MSFTNGHUB02.phx.gbl...
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/lib...rols.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.


Feb 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1641
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
12
2755
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
16974
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
1
3327
by: Darren | last post by:
hi i have got a dropdownlist in a templatecolumn and I am trying to load it with values. this is the code in the testGrid_EditCommand function testGrid.EditItemIndex = e.Item.ItemIndex;...
7
400
by: JJ | last post by:
Hi, I have a dropdownlist box that has a datasource set to a function call. the function call returns an arraylist. I moved the dropdown from a datagrid template column that I had it in and it...
2
2908
by: Marc Robitaille | last post by:
Hello, I set the DataSource property of a DropDownList to as DataSet that is filled from a SQLDataAdapter. The AutoPostBack property of that DropDownList is set to True. When the...
8
1658
by: tshad | last post by:
Can you databind a dropdownlist to another dropdownlist? I have 2 identical list. I am getting my data from a DataReader, so as soon as I have bound the first DDL, I can't do it again to the next...
1
2349
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
11
5786
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.