I have a web page where I am trying to populate the PSI # into the txtPSI depending on which customer is selected in ddlCustomers. I have written it a way it has worked on a past project and it compiles with no errors, however it doesn't work when it runs.

Protected Sub ddlCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCustomers.SelectedIndexChanged  
  'Populate PSI # in txtPSI based on Customer selected  
  Dim PSINumber As String = ""
  If ddlCustomers.SelectedIndex > 0 Then
    PSINumber = OMCustomerPSI.GetPSI(ddlCustomers.SelectedItem.ToString)  
    If PSINumber = "" Then
    MessageBox.Show("There is no PSI Number available. (Please create new PSI Number in Digecenter Manage Site.)")  
    Else
    txtPSI.Text = PSINumber  
    End If
  End If
  End Sub

If anyone can help me get this working properly I would greatly appreciate it, my whole project needs to be done EOD tomorrow. I still have a lot more to go. :( HELP!

Thanks.

Dani AI

Generated

A common cause for the symptom here is that the DropDownList never triggers a postback or its selection gets lost because the list is rebound on every Page_Load. As found, enabling the control to post back is the quick fix, but the reliable pattern is to bind the list only when the page first loads and to pass the correct value to your lookup routine. See the control property documentation for details: .

Keep the data-binding inside an If Not IsPostBack block, use SelectedValue (or the DataValueField) when the lookup expects an ID, and avoid server-side message boxes (they won't show in the browser). A minimal VB.NET pattern that avoids the usual pitfalls looks like this:

If Not IsPostBack Then
  ddlCustomers.DataSource = GetCustomers()
  ddlCustomers.DataTextField = "Name"
  ddlCustomers.DataValueField = "CustomerID"
  ddlCustomers.DataBind()
End If

Dim psi As String = OMCustomerPSI.GetPSI(ddlCustomers.SelectedValue)
If String.IsNullOrEmpty(psi) Then
  ClientScript.RegisterStartupScript(Me.GetType(), "noPsi", "alert('No PSI available');", True)
Else
  txtPSI.Text = psi
End If

If the event still doesn't fire, check these items: the DropDownList has runat="server" and the handler is wired (markup OnSelectedIndexChanged or Handles in VB), the control is not re-bound on postback, and any UpdatePanel includes an AsyncPostBackTrigger for the dropdown. For why rebinding clears events, see the ASP.NET page lifecycle notes: .

Finally, take 's point about OMCustomerPSI.GetPSI seriously: confirm the method signature and that it expects the value you supply. The ComboBox issue mentioned is the same class of problem—controls must post back and preserve their state for server events to run.

Recommended Answers

All 3 Replies

Forgot to put AutoPostBack as true

can you tell us in brif abt your question and what is OMCustomerPSI.GetPSI

That happens to me with the ComboBox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.