0

几天来我一直在与这个问题作斗争,如果有人能为我提供一些启示,我将不胜感激。

我正在根据来自另一个 dorpdownlist 的选择填充一个下拉列表。只要没有回发发生,我就会看到列表被正确填充,但是在回发之后,我的下拉列表是空的。

起初,我试图手动设置 ddl 的 Text 属性,但它一直给我以下错误:

System.ArgumentOutOfRangeException:..ddl 有一个无效的 SelectedValue,因为它不存在于列表中

然后我在某处读到我需要设置 DataValueField 属性,所以我做了消除错误的操作,但现在我在回发后看到了空的 dll。
ddl 的 ViewStateMode 设置为 Enabled。

我所指的两个 ddls 在我的 .aspx 文件的 Formview 中定义为 ddlCompanyBuyerNVListInsert 和 ddlCompanyNameListInsert(我只列出了插入模式)。基于在 ddlCompanyBuyerNVList 中所做的选择,我正在填充 ddlCompanyNameList。

下面是 Page_Load 的代码和填充 ddlCompanyNameList 的函数。

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

    If Not IsPostBack Then

        If NavHelper.User.UserName = "" Then
            Dim UserIP As String
            Dim UserLogin As String
            Dim UserEmail As String
            UserIP = HttpContext.Current.Request.UserHostAddress
            UserLogin = HttpContext.Current.Session("Username")
            UserEmail = HttpContext.Current.Session("Email")
            GetUserInfo()

            CurrentRFQ = Nothing
            If NavHelper.RFQ.ID = -1 Then
                formview_RFQ.ChangeMode(FormViewMode.Insert)
                tabpanelCustomerParts.Visible = False
                tabpanelDocuments.Visible = False
                tabpanelReviews.Visible = False
                tabpanelRFQReviewHistory.Visible = False
                listview_CustomerParts.Dispose()

            Else
                formview_RFQ.ChangeMode(FormViewMode.Edit)
                listview_ReviewContracts_Initial.EditIndex = 0
                SessionHelper.CurrentObject = TAA.Library.RFQ.GetObject(NavHelper.RFQ.ID)
                mRFQ = DirectCast(SessionHelper.CurrentObject, TAA.Library.RFQ)
                Dim UserdeptTotal As Long
                UserdeptTotal = HttpContext.Current.Session("DepartmentTotal")
                If formview_RFQ.FindControl("ddlCompanyBuyerNVList") IsNot Nothing Then
                    Dim ddl As DropDownList = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVList"), DropDownList)
                    FillCompanyNameDropDownList(ddl)
                End If
                tabpanelCustomerParts.Visible = True
                tabpanelDocuments.Visible = True
                tabpanelReviews.Visible = True
                tabpanelRFQReviewHistory.Visible = True
                If NavHelper.RFQ.Copy = True Then
                    SetModifyCopy()
                End If
            End If
        Else    'IsPostBack
            datasource_BuyerNVList.Dispose()
            datasource_RFQ.DataBind()
            datasource_RFQNote.DataBind()
            Dim ddl As DropDownList
            If (formview_RFQ.CurrentMode = FormViewMode.Insert) Then
                ddl = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVListInsert"), DropDownList)
            ElseIf formview_RFQ.FindControl("ddlCompanyBuyerNVList") IsNot Nothing Then
                ddl = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVList"), DropDownList)
            End If
            FillCompanyNameDropDownList(ddl)
        End If
    End If
End Sub

Protected Sub FillCompanyNameDropDownList(ddlCompanyBuyerNamesList As DropDownList) 尝试使用 cn 作为新的 SqlClient.SqlConnection(DB.RFQconnection) cn.Open()

            Using cm As SqlClient.SqlCommand = cn.CreateCommand

                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "GetCompanyNamesForThisBuyer"
                cm.Parameters.AddWithValue("@BuyerName", (ddlCompanyBuyerNamesList.SelectedItem.Text))
                Dim ddlCompanyNamesList As DropDownList
                If (formview_RFQ.CurrentMode = FormViewMode.Insert) Then
                    ddlCompanyNamesList = DirectCast(formview_RFQ.FindControl("ddlCompanyNameListInsert"), DropDownList)
                Else
                    ddlCompanyNamesList = DirectCast(formview_RFQ.FindControl("ddlCompanyNameList"), DropDownList)
                End If
                Using r As New SCF.Data.SafeDataReader(cm.ExecuteReader)

                    ddlCompanyNamesList.Items.Clear()
                    ddlCompanyNamesList.Items.Add(New ListItem("Select Company"))
                    While r.Read
                        Dim li As ListItem = New ListItem()
                        li.Value = SCF.Data.SafeData.SafeInteger(r("CompanyBuyerId"))
                        li.Text = SCF.Data.SafeData.SafeString(r("CompanyName"))
                        ddlCompanyNamesList.Items.Add(li)
                    End While
                End Using
                cm.Dispose()
                cn.Close()

                ' if more than one entry found, clear the company info fields until a selection is made
                If ddlCompanyNamesList.Items.Count > 2 Then 'because we've added a "Select Company" as the first item
                    ClearCompanyInfoFields()    ' company fields are text boxes that get cleared here
                    ddlCompanyNamesList.SelectedIndex = 0   
                End If
                ddlCompanyNamesList.Enabled = True
                If ddlCompanyNamesList.Items.Count = 2 Then        ' if only one entry found populate the compny info with that one entry's selection
                    ddlCompanyNamesList.SelectedIndex = 1
                    FillCompanyInfoFields(ddlCompanyNamesList)
                ElseIf ddlCompanyNamesList.Items.Count = 0 Then    ' if nothing found use the buyer list to populate company info fields
                    ddlCompanyNamesList.Items.Clear()
                    FillCompanyInfoFields(ddlCompanyBuyerNamesList)
                    ddlCompanyNamesList.Enabled = False
                End If

            End Using
        End Using
    Catch ex As System.Exception
        SetErrorPanel("An error occured during FillCompanyNameDropDownList.")
        Dim err As New HealthMonitoringCustomEvents.ErrorEvent("An error occured during FillCompanyNameDropDownList", Me, ex)
        err.Raise()
    End Try

End Sub

Protected Sub FillCompanyInfoFields(ddl As DropDownList) Try Dim BuyerTitle As TextBox Dim BuyerEmail As TextBox Dim BuyerPhone As TextBox Dim BuyerExt As TextBox Dim BuyerMobile As TextBox Dim BuyerFax As TextBox Dim CoAddr As TextBox Dim CoCity As TextBox Dim CoState As TextBox Dim CoZip As TextBox

        If (formview_RFQ.CurrentMode = FormViewMode.Insert) Then
            buyerTitle = DirectCast(formview_RFQ.FindControl("txtBuyerTitleInsert"), TextBox)
            buyerEmail = DirectCast(formview_RFQ.FindControl("txtBuyerEmailInsert"), TextBox)
            buyerPhone = DirectCast(formview_RFQ.FindControl("txtBuyerPhoneNumberInsert"), TextBox)
            buyerExt = DirectCast(formview_RFQ.FindControl("txtExtInsert"), TextBox)
            buyerMobile = DirectCast(formview_RFQ.FindControl("txtBuyerMobileNumberInsert"), TextBox)
            buyerFax = DirectCast(formview_RFQ.FindControl("txtBuyerFaxNumberInsert"), TextBox)
            CoAddr = DirectCast(formview_RFQ.FindControl("txtCompanyAddressInsert"), TextBox)
            CoCity = DirectCast(formview_RFQ.FindControl("txtCompanyCityInsert"), TextBox)
            CoState = DirectCast(formview_RFQ.FindControl("txtCompanyStateCodeInsert"), TextBox)
            CoZip = DirectCast(formview_RFQ.FindControl("txtCompanyZipCodeInsert"), TextBox)
        Else
            buyerTitle = DirectCast(formview_RFQ.FindControl("txtBuyerTitle"), TextBox)
            buyerEmail = DirectCast(formview_RFQ.FindControl("txtBuyerEmail"), TextBox)
            buyerPhone = DirectCast(formview_RFQ.FindControl("txtBuyerPhoneNumber"), TextBox)
            buyerExt = DirectCast(formview_RFQ.FindControl("txtExt"), TextBox)
            buyerMobile = DirectCast(formview_RFQ.FindControl("txtBuyerMobileNumber"), TextBox)
            buyerFax = DirectCast(formview_RFQ.FindControl("txtBuyerFaxNumber"), TextBox)
            CoAddr = DirectCast(formview_RFQ.FindControl("txtCompanyAddress"), TextBox)
            CoCity = DirectCast(formview_RFQ.FindControl("txtCompanyCity"), TextBox)
            CoState = DirectCast(formview_RFQ.FindControl("txtCompanyStateCode"), TextBox)
            CoZip = DirectCast(formview_RFQ.FindControl("txtCompanyZipCode"), TextBox)
        End If

        Using cn As New SqlClient.SqlConnection(DB.RFQconnection)
            cn.Open()
            Using cm As SqlClient.SqlCommand = cn.CreateCommand
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "GetBuyerCompanyInfo"
                cm.Parameters.AddWithValue("@companyBuyerId", (ddl.SelectedValue))
                Using r As New SCF.Data.SafeDataReader(cm.ExecuteReader)
                    ' populate company information fields in CurrentRFQ
                    While r.Read
                        CurrentRFQ.CompanyID = SCF.Data.SafeData.SafeString(r(0))
                        CurrentRFQ.CompanyBuyerId = SCF.Data.SafeData.SafeString(r(7))
                        ddl.DataValueField = SCF.Data.SafeData.SafeString(r(1))
                        CoAddr.Text = SCF.Data.SafeData.SafeString(r(2))
                        CoCity.Text = SCF.Data.SafeData.SafeString(r(4))
                        CoState.Text = SCF.Data.SafeData.SafeString(r(5))
                        CoZip.Text = SCF.Data.SafeData.SafeString(r(6))
                        buyerTitle.Text = SCF.Data.SafeData.SafeString(r(9))
                        buyerEmail.Text = SCF.Data.SafeData.SafeString(r(10))
                        buyerPhone.Text = SCF.Data.SafeData.SafeString(r(11))
                        buyerExt.Text = SCF.Data.SafeData.SafeString(r(12))
                        buyerMobile.Text = SCF.Data.SafeData.SafeString(r(13))
                        buyerFax.Text = SCF.Data.SafeData.SafeString(r(14))
                    End While
                End Using
                cm.Dispose()
                cn.Close()
            End Using
        End Using

    Catch ex As System.Exception
        SetErrorPanel("An error occured during FillCompanyInfoFields.")
        Dim err As New HealthMonitoringCustomEvents.ErrorEvent("An error occured during FillCompanyInfoFields", Me, ex)
        err.Raise()
    End Try
End Sub

以下是在 .aspx 文件中定义两个 ddls 的方式。

<td> <asp:DropDownList ID="ddlCompanyBuyerNVListInsert" runat="server"
 AppendDataBoundItems="true" AutoPostBack="true" CssClass="Input" 
 DataSourceID="datasource_BuyerNVlist" DataTextField="Key"  DataValueField="Value" 
 OnSelectedIndexChanged="ddlCompanyBuyerNVListInsert_SelectedIndexChanged"
 ViewStateMode="Enabled" Width="205px"> <asp:ListItem Selected="True" Text="Select
 Buyer" Value="0" /> </asp:DropDownList> </td> <td></td> <td class="style4"> Company:
 </td> <td>
<asp:DropDownList ID="ddlCompanyNameListInsert" runat="server"
AppendDataBoundItems="True" AutoPostBack="True" CssClass="Input"onselectedindexchanged="ddlCompanyNameListInsert_SelectedIndexChanged"
ViewStateMode="Enabled" Enabled="False"> </asp:DropDownList> </td>

非常感谢你的回复。

4

0 回答 0