Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist doesn't display selected value 2

Status
Not open for further replies.

wlhbill

Programmer
Aug 25, 2002
20
0
0
US
I have a page with user information, name, address, city, state, zip, etc. The state field is a dropdownlist populated with statecode and statename. When the page is initially linked to it accesses a database to retrieve a previously determined record and populates the fields on the page. It displays everything correctly except the state code. This is the code that populates the page:
if (usr!=null)
{
txtFirstName.Text=usr.FName;
txtLastName.Text=usr.LName;
txtAddress.Text=usr.Address;
txtCity.Text=usr.City;
string st=usr.State;
ListItem li=cboStateCode.Items.FindByValue(st);
if(li != null)
cboStateCode.SelectedIndex=cboStateCode.Items.IndexOf(li);
txtZip.Text=usr.Zip;
txtPhone.Text=usr.Phone;
txtEMail.Text=usr.EMail;
string lsContactBy=usr.ContactBy;
rblContact.SelectedValue=lsContactBy;
}

The statecode field is displaying the first item in it's list instead of the one defined by usr.state. I have traced the steps thru debug and the index is getting set to the right value.

The last field, rblContact, is a three radio button control and it is displayed correctly.

I'm puzzled!

Thanks for any suggestions.
Bill
 
If you already have this:
string st=usr.State;
Can't you just:
cboStateCode.SelectedValue = st;
Or am I missing something?
 
Thanks for the reply Veep,

That certainly seems to be a much easier way to do it but unfortunatlly the results are the same. I just tried it and it still displays the first item in the list (Alabama) instead of 'st' (Florida).

???
 
Are you calling Page.DataBind() method? Because I don't see a cboStateCode.DataBind() in your code ...

[morning]
 
The cboStateCode object is a separately defined web object. The databind occurs when the object is created. The list contains all the items, it just displays the first item instead of the selected one. I can include the code defining the cboStateCode object if you would like.

Thanks,
Bill
 
The databind occurs when the object is created
Where does this happen? Is it the Page Load and it is simply being bound again (rather than checking if it is a postback)?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
The statecode object is declared with:
protected HssWebControls.StateCode cboStateCode;

The StateCode class is defined in a separate dll HssWebControls with connection and binding to the database. The cboStateCode control works fine with the exception of initially displaying the right entry before any postback.

I am building this web site as an exercise for me to learn asp.net and c#. I've been a non-web application developer for 30 years and I don't want to get left too far behind in the dust. :) My web address is hss.homeip.net. If you go to the 'Leave a comment' page you can try it and see what I'm talking about.
Thanks,
Bill
 
Hi,

I've been on your site, registered and submitted an entry. When I log back in you are correct in that it shows the first entry for the state rather than the one I selected.

To me, it looks as though the drop down list is being repopulated after your code that sets the selected index. Have you debugged your code to see what order these events occur at? I would hazard a guess that this is the cause of the problem although I could be wrong.

Hope this helps.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Stepping through in debug mode the first thing that happens is InitializeComponent(); (this assigns event handlers to the various controls)
base.OnInit(e); (does default stuff I think)
Code:
private void FormInit() 
{
  if (Session["CurrentUser"]!=null)
  {
    sID=Session["CurrentUser"].ToString();
  }
  HttpCookie cookie=new HttpCookie("hssWeb");
  cookie=Request.Cookies["hssWeb"];
  cbRemember.Checked= (cookie !=null);
  if (sID=="0")
  {
    if (cookie != null)
    {
      sID=cookie.Values["id"].ToString();
    }
  }
  usr=null;
  usr=HssUser.GetOne("id",sID); //This will retrieve one user record
  if (usr != null)
  {
    us=UserState.Load(usr.Id); //UserState retreives logged in user name, id, and status
    if (us.LoggedIn)
    {
      Session["CurrentUser"]=usr.Id;
      DataView comment=Comments.GetUserComments(usr.Id);
      btnComments.Visible=(comment.Count!=0);
    }
    else
    {
      us=UserState.Load(0);
      btnComments.Visible=false;
    }
  }
  else
  {
    us=UserState.Load(0);
  }
  if (IsPostBack)
  {   // saves the screen data in struct scr for use in updating
    us=UserState.Load(Convert.ToInt32(sID));
    scr.email=Request.Form["txtEMail"];
    scr.fname=Request.Form["txtFirstName"];
    scr.lname=Request.Form["txtLastName"];
    scr.address=Request.Form["txtAddress"];
    scr.city=Request.Form["txtCity"];
    scr.state=Request.Form["cboStateCode"];
    scr.zip=Request.Form["txtZip"];
    scr.phone=Request.Form["txtPhone"];
    scr.contact=Request.Form["rblContact"];
  }
}
'On this pass it is not postback but the user is logged in'

next is PageLoad
Code:
private void Page_Load()
{
  if (us.UserName!="None")
  {
    GetUser("username",us.UserName);
  }
  btnLogin.Visible=(us.Role=="None");
  btnLogout.Visible=!btnLogin.Visible;
  HyperLink2.Visible=(us.Role=="Admin");
}

private void GetUser(string keyName,string keyValue)
{
  HssUser usr=HssUser.GetOne(keyName,keyValue);
  if (usr!=null)
  {
    txtFirstName.Text=usr.FName;
    txtLastName.Text=usr.LName;
    txtAddress.Text=usr.Address;
    txtCity.Text=usr.City;
    string st=usr.State;
    cboStateCode.SelectedValue=st;
    txtZip.Text=usr.Zip;
    txtPhone.Text=usr.Phone;
    txtEMail.Text=usr.EMail;
    string lsContactBy=usr.ContactBy;
    rblContact.SelectedValue=lsContactBy;
  }
}
At this point the page displays with the wrong state displayed but debug shows the index is pointing to the right entry. This is the only place the screen controls are populated and it only went thru it one time. ??
 
If the cboStateCode.SelectedValue=st line shows that the correct index is being selected then the only thing I can think of is that the data binding for the control must happen afterwards.

You mentioned in a previous post that:
The StateCode class is defined in a separate dll HssWebControls with connection and binding to the database.
Have you noticed in debug when this procedure is called?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yeaaa! That was it!!
The onInit method of the StateCode control was populating the list and did a databind. This was good and where it was supposed to happen. I also had a databind in the onload event. Suspenders and a belt, right. Unfortunatlly the onload event of the control was fired after the onload event of the page which reset the index even though the debug display didn't show it.

Thank you very much ca8msm for hanging in there with me and keeping me pointed in the right direction. If you try the site now it will show the statecode you entered instead of AL.
 
now it will show the statecode you entered instead of AL
So it does...glad to be of help.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top