asp.net DropDownList回发没有在第一次回发时执行方法

时间:2016-12-23 15:27:52

标签: asp.net vb.net postback

我突然面临非常不自然的问题。我有DropdownList与autopostback是真的。回发执行一种方法,根据选择在页面上填充其他内容。现在,当我从该下拉列表中第一次选择任何值时,页面将获得回发但没有任何内容可以填充,但从第二次起,它可以正常工作。即使我把断点放在那个下拉列表上第一次回发甚至没有击中断点。

<asp:DropDownList ID="ClientCode" runat="server" ClientIDMode="Static" CssClass="field-pitch" AutoPostBack="true"></asp:DropDownList>

Private Sub ClientCode_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClientCode.SelectedIndexChanged
        Me.populateConsignerDetails()
End Sub

Private Sub populateConsignerDetails()
        Try
            Dim str As String = "SELECT * FROM clientsDetails WHERE clientID = @clientID"
            con.Open()
            Dim cmd As New MySqlCommand(str, con)
            cmd.Parameters.AddWithValue("@clientID", ClientCode.SelectedItem.ToString)
            Dim da As New MySqlDataAdapter(cmd)
            Dim dt As New DataTable
            da.Fill(dt)
            con.Close()
            Dim payingParty As String = String.Empty
            If dt.Rows.Count > 0 Then

                consignerName.Text = dt.Rows(0)("clientName").ToString
                consignerAddress.Text = dt.Rows(0)("companyAddress").ToString
                consignerMobile1.Text = dt.Rows(0)("contactNumber1").ToString
                consignerCity.Text = dt.Rows(0)("city").ToString
                consignerState.Text = dt.Rows(0)("state").ToString
                consignerPinCode.Text = dt.Rows(0)("pinCode").ToString
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End Sub

更新

Private Sub myadmin_shipment_details2_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            populateClient()
        End If
End Sub

Private Sub populateClient()
        Using conn As New MySqlConnection()
            conn.ConnectionString = ConfigurationManager _
                .ConnectionStrings("conio").ConnectionString()
            Using cmd As New MySqlCommand()
                cmd.CommandText = "Select * from clientsDetails where status = 'active'"
                cmd.Connection = conn
                conn.Open()
                Using sdr As MySqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        Dim item As New ListItem()
                        item.Text = sdr("clientID").ToString()
                        item.Value = sdr("ClientName").ToString()
                        ClientCode.Items.Add(item)
                    End While
                End Using
                conn.Close()
            End Using
        End Using
    End Sub

0 个答案:

没有答案