473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DropDownList SelectedIndex Property

I have a DropDownList inside a DataList on a page. I want to set the
selectedindex property of the DropDownList to some value.

I bind the DropDownList on the ItemCreated Event of the DataList.

Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.DataSource = GetDropDownListData
ddl.DataBind()
ddl.SelectedIndex = 2
end sub

but this does not sets the selected index and the selected index comes
out to be default ( i.e 0)

Where should i set this property so that it displays on the rendered code

Any help would be appreciated

TIA
-ashish
Nov 18 '05 #1
3 8753
Hi,

run this code in ItemDataBound instead of ItemCreated. In ItemCreated,
DataListItem itself is not yet added to the Controls collection, which means
its child controls are neither. This means that changes made to the control
state, like databinding, are not stored to ViewState. Though you are doing
this in ItemCreated, when it is run on every request and should otherwise
replace the databinding behaviour made in ItemDataBound, but rebinding the
grid will put the SelectedIndex back to 0. Besides ItemCreated can run twice
on same request (when DataList is restored from ViewState as well as
rebound), so databinding multiple times is actually also a waste of
resources.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Ashish Sharma" <as****@mailmenot.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
I have a DropDownList inside a DataList on a page. I want to set the
selectedindex property of the DropDownList to some value.

I bind the DropDownList on the ItemCreated Event of the DataList.

Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.DataSource = GetDropDownListData
ddl.DataBind()
ddl.SelectedIndex = 2
end sub

but this does not sets the selected index and the selected index comes
out to be default ( i.e 0)

Where should i set this property so that it displays on the rendered code

Any help would be appreciated

TIA
-ashish
Nov 18 '05 #2
Hi,

run this code in ItemDataBound instead of ItemCreated. In ItemCreated,
DataListItem itself is not yet added to the Controls collection, which means
its child controls are neither. This means that changes made to the control
state, like databinding, are not stored to ViewState. Though you are doing
this in ItemCreated, when it is run on every request and should otherwise
replace the databinding behaviour made in ItemDataBound, but rebinding the
grid will put the SelectedIndex back to 0. Besides ItemCreated can run twice
on same request (when DataList is restored from ViewState as well as
rebound), so databinding multiple times is actually also a waste of
resources.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Ashish Sharma" <as****@mailmenot.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
I have a DropDownList inside a DataList on a page. I want to set the
selectedindex property of the DropDownList to some value.

I bind the DropDownList on the ItemCreated Event of the DataList.

Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.DataSource = GetDropDownListData
ddl.DataBind()
ddl.SelectedIndex = 2
end sub

but this does not sets the selected index and the selected index comes
out to be default ( i.e 0)

Where should i set this property so that it displays on the rendered code

Any help would be appreciated

TIA
-ashish
Nov 18 '05 #3
Teemu Keiski wrote:
Hi,

run this code in ItemDataBound instead of ItemCreated. In ItemCreated,
DataListItem itself is not yet added to the Controls collection, which means
its child controls are neither. This means that changes made to the control
state, like databinding, are not stored to ViewState. Though you are doing
this in ItemCreated, when it is run on every request and should otherwise
replace the databinding behaviour made in ItemDataBound, but rebinding the
grid will put the SelectedIndex back to 0. Besides ItemCreated can run twice
on same request (when DataList is restored from ViewState as well as
rebound), so databinding multiple times is actually also a waste of
resources.

hey Thanks for explanation Teemu,
it works now !
Nov 18 '05 #4

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

Similar topics

2
by: Ashish Sharma | last post by:
I have a DropDownList inside a DataList on a page. I want to set the selectedindex property of the DropDownList to some value. I bind the DropDownList on the ItemCreated Event of the DataList. ...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
2
by: Wee Bubba | last post by:
i have a dropdownlist consisting of titles (Mr,Mrs,etc.). I use the text property to store a title and the value properties to store a default gender code. e.g. Title Default Gender ----...
1
by: charmis | last post by:
In msdn library, DropDownList.SelectedIndex Property is defined as public override int SelectedIndex {get; set;} The return type is int. In msdn library itself the int is defined as The...
1
by: Shaun Camilleri | last post by:
Hi all, I am creating a DropDownList in a RepeaterControl. After the Repeater is DataBound in the ItemCreated event (of the Repeater) I bind the DropDownList to a Table and then try to select one...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
4
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet,...
1
by: g | last post by:
I have a user control that contains a dropdownlist. Using a public property, I can get the selected item. However, I am unsure of how to use that same public property to set the selected item. ...
3
by: S_K | last post by:
Hi, I have a list of 6 DropDownList boxes, from DropDownList1 to DropDownList6, and I'm changing the SelectedIndex of each using a foreach loop as follows: foreach(PaymentReqDisplay...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...

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.