DropDownList SelectedIndex Problem

Results 1 to 2 of 2

Thread: DropDownList SelectedIndex Problem

  1. #1
    Join Date
    Dec 1969

    Default DropDownList SelectedIndex Problem

    Can anyone help please, I have a dropdownlist(ddl) on my web form which I have populated successfully with data from a DataTable:<BR><BR>//this is in CodeBehind//<BR>cboUserID.DataSource = dsLookupData.Tables["UserDetails"];<BR>cboUserID.DataValueField = "UserID";<BR>cboUserID.DataTextField = "Surname";<BR>cboUserID.SelectedValue = m_strUserID.ToString();<BR>///DataBind is called later....<BR><BR>To get the value out again, this code should be all that is required (and I should be able to put it either in CodeBehind or in the aspx):<BR>m_strUserID = cboUserID.SelectedValue;<BR><BR>The problem is, when I then try to save changes to the record, the SelectedIndex for the ddl is always -1 (i.e no selection). The data from my text boxes comes through fine AND when I set &#060;%@ Page Trace="true" &#062; I can see the value that I selected in the ddl under "Form Collection". I&#039;ve tried many different ways of ensuring this value is set, I&#039;ve moved the code which creates the DataTable and sets the DataSource out of the codebehind and into the .aspx in a script and have tried changing when I perform the DataBind, but to no avail. <BR><BR>The only time I can get SelectedIndex to be anything other than -1 is when I explicitly set the values by adding each new row individually, but this is no good, the data has to come from the database. <BR><BR>This should be a simple thing, have a got a setting turned off somewhere? ViewState is NOT turned off. The only thing I can think of is that I am still using .NET 2003 BETA (version 7.1.2215) and that this is an actual problem in the Beta version? Can anyone confirm this theory for me?<BR><BR>Any guidance is greatly appreciated!<BR><BR>Somewhat Frustrated,<BR><BR>Walaus

  2. #2
    Join Date
    Dec 1969

    Default RE: DropDownList SelectedIndex Problem

    I take it you are saving records in the event handler of a button or something like that?<BR><BR>If so, then your page load will be getting run AGAIN before the event handler.<BR><BR>So add the following to your page load sub to wrap all it&#039;s current content<BR><BR>if not page.ispostback then<BR>&#039; your existing code here<BR>end if<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts