473,471 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dropdownlist.selectedItem not working from in a datalist

Hi,

I have a dropdownlist in my DataList control and I am trying to set the
SelectedItem property of the dropdownlist so when the datalist displays
the user will see the text in the dropdownlist set to a predetermined
value.

I can set the SelectedItem property easy enough and get the
dropdownlist to work fine just so long as the dropdownlist is just on
the form itself not in a DataList control.

For some reason when its in a DataList control the dropdownlist will
show the first item in the list and not the one selected.

I am setting the selectedItem property from the datalist's ItemCreated
command.
Here is the code from the item created event:

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

Dim ddlCategory As DropDownList

Try

ddlCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList)

Dim myDataSet As New DataSet
Dim strSQL As String = "SELECT CategoryID, CategoryName
FROM Category"
Dim myAdapter As New OleDbDataAdapter(strSQL,
c.Connection)

myAdapter.Fill(myDataSet)
ddlCategory.DataSource = myDataSet
ddlCategory.DataTextField = "CategoryName"
ddlCategory.DataValueField = "CategoryID"
ddlCategory.SelectedValue = myvalue
ddlCategory.DataBind()
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
End If

Right after this code has ran I have checked the SelectedValue text and
it is set to myvalue eg.
dim strTest as string = ddlCategory.SelectedValue.ToString

strTest does equal myvalue, however when the grid displays it is always
set to the first value generated from the dataset and not the
selectedvalue.

Any ideas on how to set the SelectedValue from in a DataList?

Thanks

Nov 19 '05 #1
3 2677
Check page load event.
If Page.IsPostBack property is true, then you shouldn't re-bind and
initialize your database stuff.

Burak SARICA

"sling blade" <co*************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi,

I have a dropdownlist in my DataList control and I am trying to set the
SelectedItem property of the dropdownlist so when the datalist displays
the user will see the text in the dropdownlist set to a predetermined
value.

I can set the SelectedItem property easy enough and get the
dropdownlist to work fine just so long as the dropdownlist is just on
the form itself not in a DataList control.

For some reason when its in a DataList control the dropdownlist will
show the first item in the list and not the one selected.

I am setting the selectedItem property from the datalist's ItemCreated
command.
Here is the code from the item created event:

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

Dim ddlCategory As DropDownList

Try

ddlCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList)

Dim myDataSet As New DataSet
Dim strSQL As String = "SELECT CategoryID, CategoryName
FROM Category"
Dim myAdapter As New OleDbDataAdapter(strSQL,
c.Connection)

myAdapter.Fill(myDataSet)
ddlCategory.DataSource = myDataSet
ddlCategory.DataTextField = "CategoryName"
ddlCategory.DataValueField = "CategoryID"
ddlCategory.SelectedValue = myvalue
ddlCategory.DataBind()
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
End If

Right after this code has ran I have checked the SelectedValue text and
it is set to myvalue eg.
dim strTest as string = ddlCategory.SelectedValue.ToString

strTest does equal myvalue, however when the grid displays it is always
set to the first value generated from the dataset and not the
selectedvalue.

Any ideas on how to set the SelectedValue from in a DataList?

Thanks

Nov 19 '05 #2
I have a "If Not IsPostBack is True Then BindGrid" routine in my Page
Load event for the DataList.

Once the DataList binds then the ItemCreated event fires and I create a
oledbcommand object and use it to bind stuff to the dropdownlist.

Does this sound like the right procedure?

Nov 19 '05 #3
Not just wrong. But if your dropdownlists doesn't change row by row, it
creates useless traffic and cpu time. So fill a global dataTable for the
dropdowns, and only bind them in itemcreate event.

"sling blade" <co*************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a "If Not IsPostBack is True Then BindGrid" routine in my Page
Load event for the DataList.

Once the DataList binds then the ItemCreated event fires and I create a
oledbcommand object and use it to bind stuff to the dropdownlist.

Does this sound like the right procedure?

Nov 19 '05 #4

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

Similar topics

4
by: Ivan | last post by:
Hi, I have a problem with a page which has about 50 web controls , 12 of the are DropDownList. Also, I have a "Continue" button, and on the server code an event sub btnContinue_Click, where I...
0
by: Ashish Sharma | last post by:
I have a drop down list inside a data list on a form like this : <asp:DataList ID="DataList1" runat="server" EnableViewState="False"> <ItemTemplate> <asp:Label ID="lblname" Runat="server"...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
1
by: B. Sockel | last post by:
I have created a data list and binded it to a data source with no problem, Where i am running into a problem is when i dry and bind a drop down list in Edit mode to a data source. The trick is...
6
by: Julius Fenata | last post by:
Dear all, I have created client-side scripting to trigger event onChange from code-behind, like this: DropDownList1.Attributes = "GenerateArticleID()"; At the script on Windows Form, I...
1
by: Paul L | last post by:
Hi, I have an issue with the OnSelectedIndexChanged event not firing for a DropDownList control which is in the ItemTemplate of a DataList. I have made an exact copy of the DropDownList control,...
3
by: Sam C | last post by:
Hi, I have an ASP.Net page which has a DropDownList on it. The DDL is populated via a method which is called from the Page_Load if IsPostBack = False. When the form is submitted the...
2
by: HH | last post by:
Hi, I have a dropdown list that is datafilled via a SQL table. The text part is always unique. (A list of countries) Each country is assigned one of three numbers. (This being the 'value' of...
2
by: jeffmagill | last post by:
Ive stumbled into one of those infamous problems where something stopped working for apparently no reason - in this case MyDropDownList.SelectedItem.Value. Im not sure exactly which group this...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.