Hi News,
As you said, you have the same problem as larrya. Thanks for your
information.
Based on my experience, the problem may occur under the environment of you
use code like this:
private void Page_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="job_desc";
DropDownList1.DataValueField="job_id";
DropDownList1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=DropDownList1.SelectedItem.Text;
}
Whenever you select which item in dropdownlist, if you click button and
postback, you will find Label always show the first item Text.(Also, the
dropdownlist re-initialized with first item being selected.)
=============================================================
I think this is what you and larrya's concern about state maintain.
Because each time when you postback, all the code in Page_Load will
execute. So the DropDownList will retrieve data from dataset and
re-databind again.
So the dropdownlist will return to its original state.
Actually, when page loading, you should determine whether it is the first
time visit and only initialize the dropdownlist at the first time, Iike
this:
private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack==false)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="job_desc";
DropDownList1.DataValueField="job_id";
DropDownList1.DataBind();
}
}
============================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.
Thank you for your patience and cooperation. If I misunderstand you, please
feel free to tell me in the group. I am standing by to be of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.