asp:dropdownlist , ViewState Problem

H

Harry

Hi,

Quick question for the clever ones out there!

- I have two dropdown lists on one page.
- They are getting values from a SQL databse.
- When a user select's a a value from the first dropdown the
"OnSelectedIndexChanged" is fired.
- This event updates a asp:label with a Value. (In this case a
decimal)

Problem is, I want the user to now select a value from the second
dropdown, but the first dropdown resets itself to the first value in
the dropdown list.
I have enabled "viewstate" to True, but this does not seem to make any
difference.

Has anyone got any ideas?

Thanks in advance
H


protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack);
****Extract *****
ddlCDs.DataSource = GetCDs.ExecuteReader();
ddlCDs.DataTextField = "Name" ;
ddlCDs.DataValueField = "Price";
ddlCDs.DataBind();
myConnection.Close();

lblPrice.Text = startprice.ToString();
}

protected void Drop_Change(Object Src, EventArgs E)
{
decimal TotalCost = Convert.ToDecimal(ddlCDs.SelectedItem.Value) +
Convert.ToDecimal(ddlDVDs.SelectedItem.Value);

lblPrice.Text = TotalCost.ToString();

}



**** Code from Page ****
<asp:dropdownlist ID="ddlCDs" CssClass="TextBoxes"
EnableViewState="true" DataTextFormatString="{0:c}"
OnSelectedIndexChanged="Drop_Change" AutoPostBack="true"
runat="server"></asp:dropdownlist>

<asp:dropdownlist ID="ddlDVDs" CssClass="TextBoxes"
EnableViewState="true" DataTextFormatString="{0:c}"
OnSelectedIndexChanged="Drop_Change" AutoPostBack="true"
runat="server"></asp:dropdownlist>
 
F

Fred Hirschfeld

At first glance, you have a ; (Semi-colon) at the end of your if
(!IsPostBack) where you need to have this if be a block that encompasses:

protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack)
{
// ****Extract *****
ddlCDs.DataSource = GetCDs.ExecuteReader();
ddlCDs.DataTextField = "Name" ;
ddlCDs.DataValueField = "Price";
ddlCDs.DataBind();
myConnection.Close();
}

lblPrice.Text = startprice.ToString();
}
 
I

intrader

Fred said:
At first glance, you have a ; (Semi-colon) at the end of your if
(!IsPostBack) where you need to have this if be a block that encompasses:

protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack)
{
// ****Extract *****
ddlCDs.DataSource = GetCDs.ExecuteReader();
ddlCDs.DataTextField = "Name" ;
ddlCDs.DataValueField = "Price";
ddlCDs.DataBind();
myConnection.Close();
}

lblPrice.Text = startprice.ToString();
}
Yes, the way it is coded you will databinding fresh every time that a post
occurs.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top