473,769 Members | 2,240 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 8786
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
16911
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
10771
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
7421
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
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10051
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...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
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
7413
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
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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

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.