asp:dropdownlist , Only Selects first Value Problem

H

Harry

Hi,

Just seeing if anyone can help me with a problem I am having with drop
down lists.

- I have a asp:dropdownlist that gets its values from a database.
- The user then selects a value then presses the submit button.
- The submit button then calls a function that sets a asp:lable to
that value selected.
- PROBLEM: No matter what value the user selects, the asp:lable always
sets to the first value in the dropdown box!

I am guessing the problem is that on PostBack the data is binding
again and that is when the value is being written to the lable.
(That's my beginners diagnosis so it is probaly tottally wrong!)

Any help as always, appriciated.
Thanks
H

p.s. - Am using SQL (MSDE) + Dreamweaver MX.
***** Extract(c#) *****
<script runat="server">
void ShowResult(Object Sender, EventArgs e) {
if (IsPostBack);
string ethtext = ddlEthnic.SelectedItem.Value;
lblethnic.Text = ethtext;
}
</script>

<asp:dropdownlist ID="ddlEthnic" runat="server"
DataSource="<%# dsEthnic.DefaultView %>"
DataValueField="ethnic_group">
</asp:dropdownlist>

<asp:label ID="lblethnic" runat="server"></asp:label>

<asp:button ID="btnSubmit"
OnClick="ShowResult" runat="server" Text="Search Records" />

**********************
 
A

Ather Ali Shaikh

are you filling dropdownlistbox at page load If yes then
Put the code in

if not isPostBack then
FillDropDownListBox
end if

Hope it will must work. because your Fill is not in the defined code. When
you press the button and the button is postback then the DropdownListbox
again fill first and give you the first value.

above code will protect to fill again..

Regards
Ather Ali Shaikh
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top