DropDownList SelectedIndex stuck at -1 after IsPostBack

  • Thread starter Thread starter Matthew
  • Start date Start date
M

Matthew

After selecting an option from the DropDownList and posting back, the
SelectedIndex for the DropDownList (ddlShippingMethods) remains at -1.
This form inherits from a base class. There is no code in the base
class that pertains to the dataset, ddl, datasource, etc. Any ideas
as to why this isn't working?

******Code******
Public Class frmPaymentOptions
Inherits clsMlknCsTestBase

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim dsShippingMethods As DataSet

dsShippingMethods = MyBase.prpCommerceObject.getShippingMethods

Me.ddlShippingMethods.DataTextField =
clsShippingMethodsConstants.cstrShippingMethodName

Me.ddlShippingMethods.DataValueField =
clsShippingMethodsConstants.cstrShippingMethodId

Me.ddlShippingMethods.DataSource = dsShippingMethods


If Not IsPostBack Then
Me.ddlShippingMethods.DataBind()
End If
 
OK, figured it out.

I was working with a webform that contains child controls that were
created in the CreateChildControls() sub. The dropdownlist lost it's
index because the ViewState was looking here and on postback, was
lost. The answer to this problem is creating the child controls in
OnInit. Apparantly, when the bind is in OnInit, which occurs before
CreateChildControls(), the ViewState is left intact and the DDL index
remains. The code is as follows:

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
InitializePageControls()
End Sub

Sub InitializePageControls()
Try
Dim frmPageForm As HtmlForm 'form for page
Dim tblMaster As Table
Dim trowHeader As TableRow
Dim trowSubHeader As TableRow
Dim trowContent As TableRow

'All your code here.....

MyBase.CreateChildControls()

Catch err As Exception
Response.Write(err.ToString)
End Try
End Sub
 
Back
Top