Problem with DropDownLists and Post Backs

J

~john

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_EquipmentTypes);

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

}
 
T

TiSch

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_EquipmentTypes);

//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
 
J

~john

TiSch said:
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.
 
B

bruce barker

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)
 
J

~john

bruce said:
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
 
M

Mark Rae

Thanks... could you give a short example of this?

protected void Page_Init(object sender, EventArgs e)
{
DropDownList DropDownList_EquipmentTypes = new DropDownList();
// etc
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top