回发后,dropdownlist不显示任何值

时间:2013-10-14 07:19:45

标签: asp.net

我已经和这个问题争吵了几天了,如果有人为我揭开光明的话,我会非常感激。

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

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

  

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

然后我读了一些我需要设置DataValueField属性的地方,所以我做了那个摆脱错误但现在我看到回发后的空dll。 ddl的ViewStateMode设置为Enabled。

我所指的两个ddl在我的.aspx文件的Formview中定义为ddlCompanyBuyerNVListInsert和ddlCompanyNameListInsert(我只列出了Insert模式)。 根据ddlCompanyBuyerNVList中的选择,我填充了ddlCompanyNameList。

这是Page_Load背后的代码以及填充ddlCompanyNameList的函数。

受保护的子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

受保护的Sub FillCompanyNameDropDownList(ddlCompanyBuyerNamesList As DropDownList)         尝试             使用cn As New 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

受保护的Sub FillCompanyInfoFields(ddl As DropDownList)         尝试             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作为TextBox             昏暗的CoCity作为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文件中两个ddl的定义方式。

<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>

非常感谢您的回复。

0 个答案:

没有答案