473,761 Members | 6,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ItemC reated(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data ListItemEventAr gs) Handles
DataList1.ItemC reated
Dim ddl As DropDownList
ddl = CType(e.Item.Fi ndControl("Drop DownList1"), DropDownList)
ddl.DataSource = GetDropDownList Data
ddl.DataBind()
ddl.SelectedInd ex = 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 8785
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****@mailmen ot.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.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_ItemC reated(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data ListItemEventAr gs) Handles
DataList1.ItemC reated
Dim ddl As DropDownList
ddl = CType(e.Item.Fi ndControl("Drop DownList1"), DropDownList)
ddl.DataSource = GetDropDownList Data
ddl.DataBind()
ddl.SelectedInd ex = 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****@mailmen ot.com> wrote in message
news:OF******** ******@TK2MSFTN GP12.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_ItemC reated(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data ListItemEventAr gs) Handles
DataList1.ItemC reated
Dim ddl As DropDownList
ddl = CType(e.Item.Fi ndControl("Drop DownList1"), DropDownList)
ddl.DataSource = GetDropDownList Data
ddl.DataBind()
ddl.SelectedInd ex = 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
797
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. 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)
2
3122
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 is performed. How do I force the usercontrol to display the newly assigned date? I don't have this problem if I place the two dropdownlist and the year field directly on the aspx page.
2
1224
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 ---- ---------------- Capt M Col M Dr U Mr M
1
1855
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 Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647
1
16908
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 of its value. Here is the code: If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItem.AlternatingItem Then Dim ddl as DropDownList = Ctype(e.Item.FindControl("ddl"), DropDownList) With ddl
3
4377
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 be really grateful for any assistance! Thanks
10
10768
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 = true; Where endTime is a string containing "15".
4
2011
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, when the datagrid enters it's own OnPreRender, the selected index has reverted to zero. I created a debug version of the dropdown list which inherited from dropdownlist and overrode the selected index property to trace.write whenever it was...
1
7420
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. Here's my code: private string m_item = ""; public string Item { get {
3
1905
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 thispayment in listPaymentReqDisplay) { if ((string)thispayment.TaxType == "FIT") this.DropDownList1.SelectedIndex =
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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 we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.