DropDownList SelectedIndex stuck at -1 after IsPostBack

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
 
M

Matthew

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
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top