Home > Software design >  OnSelectedIndexChanged no working in aspx Page
OnSelectedIndexChanged no working in aspx Page

Time:04-19

i have this dropdownlist in my page

<asp:DropDownList ID="ddlselectTaxas" runat="server" AutoPostBack="True" CssClass="form-control" OnSelectedIndexChanged="ddlselectTaxas_SelectedIndexChanged"
EnableViewState="True" ViewStateMode="Enabled"></asp:DropDownList>

and this is the function in the vb file

Public Sub ddlselectTaxas_SelectedIndexChanged(sender As Object, e As EventArgs)
        taxas.TryGetValue(ddlselectTaxas.SelectedItem.Text, valorTxt.Text)
        valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
End Sub

I want to change the value of a textbox (valorTxt) when I select a diferent value in the dropdownlist, but the function is not firing. I dont know why, i have zero experience with VB and asp pages. I apreciate your help. thanks in advance.

EDIT

here is the full code of the page

Public Class Emitir
    Inherits System.Web.UI.Page

    Public taxas As New Dictionary(Of String, String)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CarregaValores()
            valorTxt.ReadOnly = True
        End If
    End Sub


    Private Sub CarregaValores()
        Dim dt As DataTable
        Dim objAcesso As New AcessoBD
        Dim consulta As String = "SELECT TE.COD_TAXA, TE.DESC_TAXA, TV.VL_TAXA FROM TAXAS_EXPEDIENTE TE JOIN TAXAS_EXPEDIENTE_VALOR TV ON TE.COD_TAXA = TV.COD_TAXA"
        dt = objAcesso.DataTable(consulta, CommandType.Text)
        For Each linha As DataRow In dt.Rows
            taxas.Add(linha.ItemArray(1), linha.ItemArray(2))
        Next
        valorTxt.Text = dt.Rows.Item(0).ItemArray(2)
        valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
        ddlselectTaxas.DataTextField = "DESC_TAXA"
        ddlselectTaxas.DataValueField = "COD_TAXA"
        ddlselectTaxas.DataSource = dt
        ddlselectTaxas.DataBind()
    End Sub

    Protected Sub ddlselectTaxas_SelectedIndexChanged(sender As Object, e As EventArgs)
        taxas.TryGetValue(ddlselectTaxas.SelectedItem.Text, valorTxt.Text)
        valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
    End Sub
End Class

CodePudding user response:

You don't show your code to load up the drop list.

Remember, your page load code fires EVERY time on post back.

So, if in code on page load, you load up the drop list? You need to do this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadData()
        LoadCal()
    End If

End Sub

Sub LoadData()

    Dim cmdSQL As New SqlCommand(
        "SELECT ID,(FirstName   ' '   LastName) as EmpName 
        FROM Employee ORDER BY FirstName")

    lstEmployee.DataSource = MyRstP(cmdSQL)
    lstEmployee.DataBind()

End Sub

Function MyRstP(cmdSQL As SqlCommand) As DataTable

    Dim rstData As New DataTable
    Using conn As New SqlConnection(My.Settings.TEST4)

        Using cmdSQL
            cmdSQL.Connection = conn
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
        End Using
    End Using

    Return rstData

End Function

Now, in above, I was using a listbox, but they work the same as a drop down list (combo box).

but, if you do NOT place the loading up of data inside of the If Not IsPostBack?

Then what happens is you select a drop down, and say click on a button (or in your case autopostback = true, then the page load FIRES FIRST and THEN your code runs!!!

and, if your code re-loads the drop list? Then your selection is lost!!!

So, for just about ANY page you create, ALWAYS, but always use the is postback code stub in on-load - and thus your loading of controls data ONLY occurs one time. Since if you re-load every time, then you lose your changes.

CodePudding user response:

Well, I am not sure of what was happening, but I remade my page and it is working now. something was blocking the event. Thanks for your help.

  • Related