473,480 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with DropDownLists and Post Backs

I'm adding a DropDownList to my page in the code behind which is set to
AutoPostBack... The problem is it's giving me the same SelectedValue on
every post back, the value of the first item in the list... The
DropDownList itself retains the correct selected value (the option I
select in the list is the option the dropdown list retains on postback
-but- the property "SelectedValue" is always the first item in the list
for some reason and not the value that I selected from the
DropdownList. any ideas?

protected void Page_Load(object sender, EventArgs e)
{
DropDownList DropDownList_EquipmentTypes = new DropDownList();
DropDownList_EquipmentTypes.ID = "myList";
DropDownList_EquipmentTypes.DataSource =
ObjectDataSource_EquipmentTypes;
DropDownList_EquipmentTypes.DataTextField =
"EquipmentTypeDesc";
DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
DropDownList_EquipmentTypes.DataBind();
DropDownList_EquipmentTypes.AutoPostBack = true;
PlaceHolder1.Controls.Add(DropDownList_EquipmentTy pes);

//This value is always the first item in the list and not the
value selected????
string x = DropDownList_EquipmentTypes.SelectedValue;

}

Dec 18 '06 #1
5 998


On 18 Dez., 22:49, "~john" <SonVolt...@gmail.comwrote:
I'm adding a DropDownList to my page in the code behind which is set to
AutoPostBack... The problem is it's giving me the same SelectedValue on
every post back, the value of the first item in the list... The
DropDownList itself retains the correct selected value (the option I
select in the list is the option the dropdown list retains on postback
-but- the property "SelectedValue" is always the first item in the list
for some reason and not the value that I selected from the
DropdownList. any ideas?

protected void Page_Load(object sender, EventArgs e)
{

DropDownList DropDownList_EquipmentTypes = new DropDownList();
DropDownList_EquipmentTypes.ID = "myList";
DropDownList_EquipmentTypes.DataSource =
ObjectDataSource_EquipmentTypes;
DropDownList_EquipmentTypes.DataTextField =
"EquipmentTypeDesc";
DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
DropDownList_EquipmentTypes.DataBind();
DropDownList_EquipmentTypes.AutoPostBack = true;

PlaceHolder1.Controls.Add(DropDownList_EquipmentTy pes);

//This value is always the first item in the list and not the
value selected????
string x = DropDownList_EquipmentTypes.SelectedValue;

}
Hello,
you have to bind the dropdownlist to the datasource only on the first
time(If not Page.IsPostback).

Regards,
Tim

Dec 18 '06 #2

TiSch wrote:
Hello,
you have to bind the dropdownlist to the datasource only on the first
time(If not Page.IsPostback).

Regards,
Tim
I tried adding the bind() inside a if(!page.IsPostback) and what that
does is loads the dropdownlist initially but doesn't load any data into
after a post back... I'm guessing because the page IS a post-back so
bind() never gets called.

Dec 18 '06 #3
because you are creating you dropdown dynamically, you should move the
code to oninit. creating in pageload is too late to get postback values.

-- bruce (sqlwork.com)

~john wrote:
I'm adding a DropDownList to my page in the code behind which is set to
AutoPostBack... The problem is it's giving me the same SelectedValue on
every post back, the value of the first item in the list... The
DropDownList itself retains the correct selected value (the option I
select in the list is the option the dropdown list retains on postback
-but- the property "SelectedValue" is always the first item in the list
for some reason and not the value that I selected from the
DropdownList. any ideas?

protected void Page_Load(object sender, EventArgs e)
{
DropDownList DropDownList_EquipmentTypes = new DropDownList();
DropDownList_EquipmentTypes.ID = "myList";
DropDownList_EquipmentTypes.DataSource =
ObjectDataSource_EquipmentTypes;
DropDownList_EquipmentTypes.DataTextField =
"EquipmentTypeDesc";
DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
DropDownList_EquipmentTypes.DataBind();
DropDownList_EquipmentTypes.AutoPostBack = true;
PlaceHolder1.Controls.Add(DropDownList_EquipmentTy pes);

//This value is always the first item in the list and not the
value selected????
string x = DropDownList_EquipmentTypes.SelectedValue;

}
Dec 19 '06 #4

bruce barker wrote:
because you are creating you dropdown dynamically, you should move the
code to oninit. creating in pageload is too late to get postback values.

-- bruce (sqlwork.com)

Thanks... could you give a short example of this? That would be greatly
appreciated.

~john

Dec 19 '06 #5
"~john" <So********@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
Thanks... could you give a short example of this?
protected void Page_Init(object sender, EventArgs e)
{
DropDownList DropDownList_EquipmentTypes = new DropDownList();
// etc
}
Dec 19 '06 #6

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

Similar topics

0
1185
by: Pete | last post by:
I am building an ASP.NET application and have just encountered something very strange which I would like explained if possible. In BaseForm.ascx I have the following <form> element: <form...
15
1942
by: Steven Livingstone | last post by:
I am running into the "colon in javascript" problem as discussed here http://www.bdragon.com/entries/000324.shtml. Basically, i have nested controls and get the error on a line such as : ...
6
6990
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
0
1058
by: Pete | last post by:
I am building an ASP.NET application and have just encountered something very strange which I would like explained if possible. In BaseForm.ascx I have the following <form> element: <form...
3
2321
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
0
1812
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
6
2665
by: | last post by:
Hi all, I have a bunch of dropdownlists that are populated in client-side javascript. When i do a postback I get the following error:- Invalid postback or callback argument. Event...
3
1638
by: RSH | last post by:
Hi, I am having this very bizarre occurance with 4 dropdown lists on my ASP .Net page. Each control has its own unique id. When the user selects a value from each of the dropdownlists it is...
0
696
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I have an application that includes multi panels and a series of buttons in the form of a wizard. On a normal connection it works ok. but while using sprint borad band (a fairly slow connection) it...
0
7054
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
7057
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
7102
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...
1
6756
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
7003
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
5357
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
3008
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
1310
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
199
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...

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.