locked
DataBind on DropDownList Causes SelectedValue Invalid Error RRS feed

  • Question

  • User347808464 posted

    ·         The problem concerns a web app using ASP.NET, .NET 4.5, VB.NET.

    Problem involves 2 controls on page - a RadioButtonList (rblSourceType) and a DropDownList (ddlSpecSource)

    The page is designed for a user to load a record into a SQL Server table in 1 of 2 ways:

    1.    A new record where the user responds to each control

    2.    A new record made from a copy of an existing record 

     

    When the first method is used the record is loaded successfully.

    The second method asks the user to select a record to copy from. Data is loaded from that record and the 2 controls mentioned above are fired from code behind instead of through a user's input. Both methods use the code below except only Method 2 uses the code where the condition If lngCopySpecID > 0 Then exists. The 1<sup>st</sup> method does not use this condition.

    The rblSourceType code defines the DataSource SelectCommand and the DataValueField and DataTextField for ddlSpecSource. When the second method is used the code hangs on the yellow highlighted line, ddlSpecSource.DataBind(), with the error "ddlSpecSource has a SelectedValue which is invalid because it does not exist in the list of items."

    The real problem is not that a SelectedValue does not exist but ddlSpecSource has no items at all at the point that the DataBind method is cited. (No value has been assigned at the point where the code hangs. I’ve tried it with the assigned value code removed and it still hangs)

    The code follows:

    Protected Sub rblSourceType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblSourceType.SelectedIndexChanged

     

            Dim lngCopySrcID As Long = 0

     

            Try

     

                ddlSpecSource.Items.Clear()

     

                If rblSourceType.SelectedValue = 1 Then

                    SpecSourceData.SelectCommand = "SELECT CustomerID, Customer FROM LUCustomers WHERE

                                                    (IsCurrent = @IsCurrent) ORDER BY Customer"

                    ddlSpecSource.DataValueField = "CustomerID"

                    ddlSpecSource.DataTextField = "Customer"

                ElseIf rblSourceType.SelectedValue = 2 Then

                    SpecSourceData.SelectCommand = "SELECT StandardsOrgID, RTrim(StandardsOrgCode) + ' - '

    + RTrim(StandardsOrganization) AS StndrdsOrg FROM

    LUStandardsOrganizations ORDER BY RTrim(StandardsOrgCode)

    + ' - ' + RTrim(StandardsOrganization)"

                    ddlSpecSource.DataValueField = "StandardsOrgID"

                    ddlSpecSource.DataTextField = "StndrdsOrg"

                ElseIf rblSourceType.SelectedValue = 3 Then

                    SpecSourceData.SelectCommand = "SELECT CustomerID, Customer FROM LUCustomers WHERE

                                                   (IsCurrent = @IsCurrent) ORDER BY Customer"

                    ddlSpecSource.DataValueField = "CustomerID"

                    ddlSpecSource.DataTextField = "Customer"

                End If

     

                ddlSpecSource.Visible = True

                If lngCopySpecID > 0 Then

                    lngCopySrcID = ddlCopyFromSrc.SelectedValue

                End If

     

                SpecSourceData.DataBind()

                ddlSpecSource.ClearSelection()

                ddlSpecSource.DataSourceID = "SpecSourceData"

                ddlSpecSource.DataBind()

                If rblSourceType.SelectedValue = 1 Then

                    If lngCopySpecID > 0 Then

                        ddlSpecSource.SelectedIndex = ddlSpecSource.Items.IndexOf(ddlSpecSource.Items.FindByValue(lngCopySrcID))

                        ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                    Else

                        ddlSpecSource.Items.Insert(0, New ListItem("- Choose Customer", "-1"))

                    End If

                ElseIf rblSourceType.SelectedValue = 2 Then

                    If lngCopySpecID > 0 Then

                        ddlSpecSource.SelectedIndex = ddlSpecSource.Items.IndexOf(ddlSpecSource.Items.FindByValue(lngCopySrcID))

                        ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                    Else

                        ddlSpecSource.Items.Insert(0, New ListItem("- Choose Standards Organization", "-1"))

                    End If

                ElseIf rblSourceType.SelectedValue = 3 Then

                    ddlSpecSource.SelectedValue = 12

                    ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                End If

     

     

            Catch ex As Exception

                Debug.Print("Error - LoadSpec.aspx.vb - rblSourceType_SelectedIndexChanged " & Chr(13) _

                            & "Msg = " & ex.Message.ToString & ". Trace:  " & ex.StackTrace)

            End Try

     

        End Sub

     

     

     

        Protected Sub ddlSpecSource_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecSource.SelectedIndexChanged

     

            Dim strCustomer As String

            Dim strStndrdsOrg As String

     

            If rblSourceType.SelectedValue = 1 Then

                Session("ssnCustomerID") = ddlSpecSource.SelectedValue

                strCustomer = ddlSpecSource.SelectedItem.Text

                Session("ssnStndrdsOrgID") = 0

            ElseIf rblSourceType.SelectedValue = 2 Then

                Session("ssnStndrdsOrgID") = ddlSpecSource.SelectedValue

                strStndrdsOrg = ddlSpecSource.SelectedItem.Text

                Session("ssnCustomerID") = 0

            End If

     

            If lngCopySpecID > 0 Then

                rblCstmrOrStndrd.Visible = False

                lblCstmrOrStndrd.Visible = False

                lblCopyFromSrc.Visible = False

                ddlCopyFromSrc.Visible = False

                lblSpecToCopy.Visible = False

                ddlSpecToCopy.Visible = False

            End If

     

            If rblSourceType.SelectedValue = 1 Then

                SetupAutoDivisions(0)

            End If

     

            lblSpecNmbr.Visible = True

            txtSpecNmbr.Visible = True

            lblRevNmbr.Visible = True

            txtRevNmbr.Visible = True

     

            If lngCopySpecID > 0 Then

                txtSpecNmbr.Text = strCopySpecNmbr

            Else

                txtSpecNmbr.Text = ""

            End If

            txtRevNmbr.Text = ""

            txtRevNmbr.Focus()

     

        End Sub

    Thank you

     

    Monday, May 7, 2018 8:52 PM

Answers

  • User347808464 posted

    I've changed the code to not have to use code behind to fire a DropDownList where the items in the list were also provided from code behind.

    I haven't found a solution to the issue itself but I have worked out a different solution for my app and it works fine with the new process.

    Thanks again for the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 11, 2018 6:41 PM

All replies

  • User-330142929 posted

    Hi Stu Henning,

    The real problem is ddlSpecSource has no items at the point that the DataBind method is cited

    According to your codes, I couldn’t reproduce your problem on my side.

    I created a test demo, it works well.

    I suggest you could post the details codes about aspx page and code behind.

    If you could post more details information, it will be more easily for us to reproduce your issue and find the solution. 

    My test demo result:

    I change the databsource of the DDL in the radio button list select_changed event.

    Best Regards.

    Abraham

    Tuesday, May 8, 2018 7:22 AM
  • User347808464 posted

    Hi Abraham,

    Thanks for the reply. I appreciate the time you are taking to help.

    Yes, when the first control is fired by a user then both controls work flawlessly.

    It appears from your example that the user is clicking a choice on the RadioButtonList. If I'm seeing your example correctly that is where my problem lies. In the Copy method the user is no longer clicking a choice in the RadioButtonList. The code behind is doing that. It is when code behind calls the first control, rblSourceType, that the problem occurs. It appears to me that the action defining the SelectCommand, DataTextField, and DataValueField don't happen quickly enough for the ddlSpecSource.DataBind() step to have records yet.

    There are 2 buttons on the page - one for making a fresh record from data the user has, the 2nd for making a new record from a copy of an existing record.

    When the user clicks the "Copy" button a DropDownList, ddlSpecToCopy, appears for the user to select the record they want to copy. When the user selects the record from ddlSpecToCopy the following occurs:

    (You can see that now, instead of the user clicking rblSourceType the code behind fires rblSourceType) (The code provided in my original submission directly follows the code submitted below)

        Protected Sub ddlSpecToCopy_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecToCopy.SelectedIndexChanged

     

            Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("cnsCoreData")

            Dim cnnCore As New SqlClient.SqlConnection(cts.ConnectionString)

     

            Dim sqlSlctdSpec As String = String.Empty

            Dim cmdSlctdSpec As SqlClient.SqlCommand

            Dim dtrSlctdSpec As SqlClient.SqlDataReader

            Dim lngSpecSrcID As Long = 0

            Dim lngCustomerID As Long = 0

            Dim lngStndrdsOrgID As Long = 0

     

            InitializeLoadSpec()

            SetForNewSpecEntry(False, False)

     

            cnnCore.Open()

     

            lngCopySpecID = ddlSpecToCopy.SelectedValue

            Debug.Print("In dSTC_SIC. lngCopySpecID = " & lngCopySpecID)

     

            sqlSlctdSpec = "SELECT SpecID, CustomerID, StandardsOrgID, SpecNmbr, SpecTitle " _

                         & "FROM Specifications WHERE (((SpecID)=" & lngCopySpecID & "));"

     

            cmdSlctdSpec = New SqlClient.SqlCommand(sqlSlctdSpec, cnnCore)

            dtrSlctdSpec = cmdSlctdSpec.ExecuteReader

     

            divActivityInfo.Visible = False

            btnSaveSpec.Visible = True

     

            lblSpecTitle.Visible = True

            txtSpecTitle.Visible = True

            rblSourceType.Visible = True

     

            If dtrSlctdSpec.HasRows Then

                While dtrSlctdSpec.Read

                    lngCustomerID = dtrSlctdSpec("CustomerID")

                    lngStndrdsOrgID = dtrSlctdSpec("StandardsOrgID")

                    txtSpecTitle.Text = dtrSlctdSpec("SpecTitle")

                    txtSpecTitle_TextChanged(txtSpecTitle, New EventArgs())

                    strCopySpecNmbr = dtrSlctdSpec("SpecNmbr")

                    If lngCustomerID > 0 Then

                        lngSpecSrcID = lngCustomerID

                        If lngCustomerID = 12 Then

                            rblSourceType.SelectedValue = 3

                        Else

                            rblSourceType.SelectedValue = 1

                        End If

                    ElseIf lngStndrdsOrgID > 0 Then

                        lngSpecSrcID = lngStndrdsOrgID

                        rblSourceType.SelectedValue = 2

                    End If

                End While

            End If

     

            rblSourceType_SelectedIndexChanged(rblSourceType, New EventArgs())

     

        End Sub

    Tuesday, May 8, 2018 2:51 PM
  • User-330142929 posted

    Hi Stu Henning,

    According to your description and code, I am not clear about your issue.

    Do you mean that you bind the ddlSpecSource datasource by ddlSpecToCopy_selectedIndexChanged event handler?

    I make a sample, it works fine on my side. I couldn’t reproduce your issue. I suggest you could create a little demo which could reproduce your issue and post codes in Github or OneDrive. I will download it and test on my side.

    If you could post more details about your information. it will be more easily for me to give you a solution.

    Here are my code snippets, wish can help you.

    GIF Demo.

    Aspx.

    <div>
                <asp:RadioButtonList ID="rblSourceType" runat="server" AutoPostBack="True">
                    <asp:ListItem>DataSource1</asp:ListItem>
                    <asp:ListItem>DataSource2</asp:ListItem>
                </asp:RadioButtonList>
                <asp:DropDownList ID="ddlSpecSource" runat="server" AutoPostBack="True" Visible="False"></asp:DropDownList>
                <asp:SqlDataSource ID="SpecSourceData" runat="server"></asp:SqlDataSource>
                <hr style="border:1px solid #ffd800;margin:10px;" />
                <asp:Button ID="Copy" runat="server" Text="Copy" />
                <asp:DropDownList ID="ddlSpecToCopy" runat="server" AutoPostBack="True" Visible="False">
                    <asp:ListItem>DataSource1</asp:ListItem>
                    <asp:ListItem>DataSource2</asp:ListItem>
                </asp:DropDownList>
            </div>
    

    Code Behind.

     Protected Sub Copy_Click(sender As Object, e As EventArgs) Handles Copy.Click
            ddlSpecToCopy.Visible = True
            ddlSpecToCopy_SelectedIndexChanged(ddlSpecToCopy, New EventArgs())
        End Sub
    
        Protected Sub ddlSpecToCopy_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecToCopy.SelectedIndexChanged
            If ddlSpecToCopy.SelectedValue = "DataSource1" Then
                rblSourceType.SelectedValue = "DataSource1"
            Else
               rblSourceType.SelectedValue = "DataSource2"
            End If
            rblSourceType_SelectedIndexChanged(rblSourceType, New EventArgs())
        End Sub
        Protected Sub rblSourceType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblSourceType.SelectedIndexChanged
            ddlSpecSource.Items.Clear()
            If rblSourceType.SelectedValue = "DataSource1" Then
                SpecSourceData.SelectParameters.Clear()
                SpecSourceData.SelectCommand = "Select * from Products where ID=@Id"
                ddlSpecSource.DataValueField = "ID"
                ddlSpecSource.DataTextField = "Name"
            Else
                SpecSourceData.SelectParameters.Clear()
                SpecSourceData.SelectCommand = "Select * from Persons where Id_P=@Id"
                ddlSpecSource.DataValueField = "Id_P"
                ddlSpecSource.DataTextField = "FirstName"
            End If
            ddlSpecSource.Visible = True
    
            SpecSourceData.ConnectionString = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
            SpecSourceData.SelectParameters.Add("Id", 1) ''add some esstential parameters to the SpecSourceData
            SpecSourceData.DataBind()
            ddlSpecSource.ClearSelection()
            ddlSpecSource.DataSourceID = "SpecSourceData"
            ddlSpecSource.DataBind()
    
        End Sub
        Protected Sub ddlSpecSource_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecSource.SelectedIndexChanged
        End Sub
    

    If you have any question, please feel free to let me know.

    Look forward to your reply.

    Best regards

    Abraham

    Wednesday, May 9, 2018 2:47 PM
  • User347808464 posted

    Hi Abraham,

    I sure appreciate the time you have spent to try to answer my issue. The code you listed won't work for me. ddlSpecSource does not dictate the values of rblSourceType. rblSourceType dictates the data that will be populated in ddlSpecSource.

    When the user decides to make a copy from an existing record a DropDownList, ddlCopyFromSrc, is made visible to choose the Spec Source. Once the Spec Source is chosen a 2nd DropDownList, ddlSpecToCopy, becomes visible for the user to select the Spec to copy. The values of these 2 DropDownLists are fed to the controls that would otherwise appear when the user was filling in the page with an entirely new Spec. (along with values for the other fields that make up a record in this table)

    1. The Source type must be chosen first - rblSourceType (once again, by the code behind not because a user clicked it)
    2. The DataSource for the DropDownList - ddlSpecSource is already known to be one of 2 predetermined sql strings. We just don't know which until the code behind assigns a value to rblSourceType from the 1st exposed DropDownList, ddlCopyFromSrc
    3. The DataSource - SpecSourceData is bound
    4. The appropriate DataText Field and DataValueField are assigned to ddlSpecSource
    5. ddlSpecSource is bound
    6. A selection is also assigned to ddlSpecSource. The value from the 2nd exposed DropDownList, ddlSpecToCopy
    7. The original 2 dropdown lists - ddlCopyFromSrc and ddlSpecToCopy are no longer needed and are hidden

    Thanks for the time. I will work on this more. I know that the issue is just that the action is not allowing the DropDownList ddlSpecSource to have data loaded before the DataBind command is issued.

    Thanks again for your time

    Thursday, May 10, 2018 4:00 AM
  • User347808464 posted

    I should clarify since I explained above the user involvement and was trying to simplify but instead confused a part of the issue.

    There are 2 DropDownLists and a RadioButtonList that get exposed to the user initially for a new record from copy of an existing record

    1. First the RadioButtonList - rblCstmrOrStndrd gets exposed - This is the value that gets assigned to rblSourceType
    2. Then DropDownList 1 gets exposed - ddlCopyFromSrc - to choose which customer or standard org - This value gets assigned to ddlSpecSource
    3. DropDownList 2, ddlSpecToCopy gets exposed for the user to choose the actual Spec to copy - from the particular customer or standard org - This value gets used to generate a sql command to get the remaining values for this record and fill-in the rest of the page's fields to complete the entry of the new record and makes all the remaining controls visible for the user to edit as needed

    The reason for the transfer of the values to other controls - rblSourceType, and ddlSpecSource is when the new record gets saved the same code to save a completely new entry is used and it looks for the values from these controls.

    The page code follows:

            <header class="page-header" style="display:inline;margin-bottom:25px;">

                <div class="row">

                    <div class="col-lg-6">

                        <h4 ID="lblPgTitle">Load New Spec </h4>

                        <h5 ID="lblPgSubTitle">Full process loads Specification, Tests defined by Spec and Testing Schemes</h5>

                    </div>

                    <div class="col-lg-4" style="margin-top:10px">

                        <p>

                            <a id="lkbCreateNewSpec" class="btn btn-primary btn-md" data-toggle="modal" data-target="#mdlEntryChk">New Spec</a>

                            <asp:Button ID="btnNewSpecFromCopy" runat="server" Text="Copy" visible="true" CssClass="btn btn-primary btn-md" />

                            <asp:Button ID="btnStartAnother" runat="server" Text="Start Another" visible="false" CssClass="btn btn-primary btn-md" />

                            <asp:Button ID="btnSpecView" runat="server" Text="Logged Specs" visible="false" CssClass="btn btn-primary btn-md" />

                        </p>

                    </div>

                    <div class="col-lg-2">

                        <img src="images/TRSLogoQ60-2.png" alt="" style="height:45px;" />

                    </div>

                </div>

            </header>

            <br />

     

            <div class="row col-lg-12 form-group">

                <div id="divActivityInfo" runat="server" visible="false" class="">

                    <h4 id="hmlMsgTitle" title="" runat="server" visible="false"></h4>

    <%--                <asp:Label ID="lblMsgTitle" runat="server" CssClass="h4 alert" Text="" Visible="false"></asp:Label>

                    <br />--%>

                    <asp:Label ID="lblMsg"  runat="server" Text="" Visible="false"></asp:Label>

                </div>

            </div>

     

            <div class="row">

                <div class="col-lg-12" style="padding-left:0px">

                    <div class="control-group">

                        <Label for="rblCstmrOrStndrd" id="lblCstmrOrStndrd" class="control-label" visible="false" runat="server">Source</Label>           

                        <asp:RadioButtonList ID="rblCstmrOrStndrd" CssClass="RadioButton RadioButtonList" runat="server"

                                                        RepeatDirection="Horizontal" AutoPostBack="true" Visible="false" RepeatLayout="Flow"

                                                        OnSelectedIndexChanged="rblCstmrOrStndrd_SelectedIndexChanged" >

                            <asp:ListItem class="radio-inline" Value="1" >Customer</asp:ListItem>

                            <asp:ListItem class="radio-inline" Value="2" >Industry Standard</asp:ListItem>

                        </asp:RadioButtonList>     

                    </div>

                </div>

            </div>

     

            <div class="row">

                <div class="col-lg-4" style="padding-left:0px">

                    <div class="form-group">

                        <asp:Label ID="lblCopyFromSrc" AssociatedControlID="ddlCopyFromSrc" CssClass="control-label" runat="server"

                                        Text="" Visible="false"></asp:Label>           

                        <asp:DropDownList ID="ddlCopyFromSrc" CssClass="form-control" DataSourceID="CopyFromSrcData"

                                                    AutoPostBack="true" DataValueField="" DataTextField="" Visible="false" runat="server"

                                                    AppendDataBoundItems="true" OnSelectedIndexChanged="ddlCopyFromSrc_SelectedIndexChanged" >

                        </asp:DropDownList>

                        <asp:SqlDataSource ID="CopyFromSrcData" runat="server"

                                                    ConnectionString="<%$ ConnectionStrings:cnsCoreData %>"

                                                    SelectCommand="">

                            <SelectParameters>

                                <asp:Parameter DefaultValue="1" Name="IsCurrent" Type="Int16" />

                            </SelectParameters>

                        </asp:SqlDataSource> 

                    </div> 

                </div>

                <div class="col-lg-8" style="padding-right:0px">

                    <div class="form-group">

                        <asp:Label ID="lblSpecToCopy" AssociatedControlID="ddlSpecToCopy" CssClass="control-label" runat="server"

                                        Text="Specification" Visible="false"></asp:Label>           

                        <asp:DropDownList ID="ddlSpecToCopy" CssClass="form-control" DataSourceID="SpecToCopyData"

                                                    AutoPostBack="true" DataValueField="SpecID" DataTextField="Spec"

                                                    AppendDataBoundItems="true" Visible="false" runat="server"

                                                    OnSelectedIndexChanged="ddlSpecToCopy_SelectedIndexChanged" >

                        </asp:DropDownList>

                        <asp:SqlDataSource ID="SpecToCopyData" runat="server"

                                                        ConnectionString="<%$ ConnectionStrings:cnsCoreData %>"

                                                        SelectCommand="">

                            <SelectParameters>

                                <asp:ControlParameter ControlID="ddlCopyFromSrc" Name="SlctdSrcID" Type="Int32" />

                            </SelectParameters>

                        </asp:SqlDataSource> 

                    </div> 

                </div>

            </div>

     

    <%--        <div class="row">

                <div class="col-lg-12" style="padding-left:0px;padding-right:0px">

                    <div class="form-group">

                        <asp:Label ID="lblStndrdToCopy" AssociatedControlID="ddlStndrdToCopy" CssClass="control-label" runat="server"

                                        Text="Supplemental Spec(s)" Visible="false"></asp:Label>           

                        <asp:DropDownList ID="DropDownList1" CssClass="form-control" DataSourceID="SpecToCopyData"

                                                    AutoPostBack="true" DataValueField="" DataTextField=""

                                                    AppendDataBoundItems="true" Visible="false" runat="server"

                                                    OnSelectedIndexChanged="ddlSpecToCopy_SelectedIndexChanged" >

                        </asp:DropDownList>

                        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

                                                        ConnectionString="<%$ ConnectionStrings:cnsCoreData %>"

                                                        SelectCommand="SELECT DISTINCT Specifications.SpecID, RTrim(Specifications.SpecNmbr)

                                                                                + ' Rev ' + RTrim(Specifications.SpecRev) AS Spec FROM Specifications

                                                                                LEFT JOIN SpecToCustomer ON Specifications.SpecID = SpecToCustomer.SpecID

                                                                                WHERE (SpecToCustomer.CustomerID=@CstmrID)

                                                                                ORDER BY RTrim(Specifications.SpecNmbr) + ' Rev ' + RTrim(Specifications.SpecRev)">

                            <SelectParameters>

                                <asp:Parameter Name="CstmrID"  Type="Int32" />

                            </SelectParameters>

                        </asp:SqlDataSource> 

                    </div> 

                </div>

            </div>--%>

     

     

            <div class="row">

                <div class="col-lg-11">

                    <div class="form-group">

                        <asp:Label ID="lblSpecTitle" AssociatedControlID="txtSpecTitle" Text="Title" Visible="false" runat="server"></asp:Label>  

                        <asp:TextBox ID="txtSpecTitle" CssClass="form-control" style="width:100%;max-width:100%" Visible="false"

                                            OnTextChanged="txtSpecTitle_TextChanged" AutoPostBack="true" runat="server" ></asp:TextBox>

                    </div>

                </div>

                <div class="col-lg-1" style="margin-right:0px;padding-left:0px;padding-right:0px;padding-top:24px;vertical-align:bottom;align-content:flex-end">

                    <asp:Button ID="btnSaveSpec" runat="server" Text="Save New" Visible="false" CssClass="btn btn-primary btn-md" />

                </div>

            </div>

     

            <%--     Start of 1st column of 2 column form format     --%>

     

            <div class="col-lg-6">

                <div class="row">

                    <div class="control-group">

                        <asp:Label ID="lblSourceType" AssociatedControlID="rblSourceType" Text="Source" Visible="false" runat="server" ></asp:Label>    

                        <fieldset>

                            <asp:RadioButtonList ID="rblSourceType" CssClass="RadioButton RadioButtonList radio.radiobuttonlist" runat="server"

                                        RepeatDirection="Horizontal" AutoPostBack="true" Visible="false" RepeatLayout="Flow"

                                        OnSelectedIndexChanged="rblSourceType_SelectedIndexChanged" >

                                <asp:ListItem class="radio-inline" Value="1" Text="Customer"></asp:ListItem>

                                <asp:ListItem class="radio-inline" Value="2" Text="Standards Organization"></asp:ListItem>

                                <asp:ListItem class="radio-inline" Value="3" Text="Harman"></asp:ListItem>

                            </asp:RadioButtonList>     

                        </fieldset>

                    </div>

                </div>

     

                <div class="row">

                    <div class="form-group">

                        <asp:DropDownList ID="ddlSpecSource" CssClass="form-control" DataSourceID="SpecSourceData"

                                                    AutoPostBack="true" DataValueField="" DataTextField=""

                                                    AppendDataBoundItems="true" Visible="false" runat="server"

                                                    OnSelectedIndexChanged="ddlSpecSource_SelectedIndexChanged" >

                        </asp:DropDownList>

                        <asp:SqlDataSource ID="SpecSourceData" runat="server"

                                                    ConnectionString="<%$ ConnectionStrings:cnsCoreData %>"

                                                    SelectCommand="">

                            <SelectParameters>

                                <asp:Parameter DefaultValue="1" Name="IsCurrent" Type="Int16" />

                            </SelectParameters>

                        </asp:SqlDataSource>

                    </div>

                </div>

    The code behind is listed following:

    Public Class SpecLoad

        Inherits System.Web.UI.Page

     

        Dim lngCopySpecID As Long = 0

        Dim lngCopySrcID As Long = 0

        Dim strCopySpecNmbr As String

     

     

        Protected Sub btnNewSpecFromCopy_Click(sender As Object, e As EventArgs) Handles btnNewSpecFromCopy.Click

     

            If IsPostBack Then

                InitializeLoadSpec()

            End If

     

            lblCstmrOrStndrd.Visible = True

            rblCstmrOrStndrd.Visible = True

     

        End Sub

     

        Protected Sub rblCstmrOrStndrd_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblCstmrOrStndrd.SelectedIndexChanged

     

            If rblCstmrOrStndrd.SelectedValue = 1 Then

                CopyFromSrcData.SelectCommand = "SELECT CustomerID AS SlctdSrcID, Customer FROM LUCustomers WHERE (IsCurrent = @IsCurrent) ORDER BY Customer;"

                ddlCopyFromSrc.DataValueField = "SlctdSrcID"

                ddlCopyFromSrc.DataTextField = "Customer"

                lblCopyFromSrc.Text = "Customer"

            ElseIf rblCstmrOrStndrd.SelectedValue = 2 Then

                CopyFromSrcData.SelectCommand = "SELECT StandardsOrgID AS SlctdSrcID, RTrim(StandardsOrgCode) + ' - '

       + RTrim(StandardsOrganization) AS StndrdOrg

                                                FROM LUStandardsOrganizations WHERE (IsCurrent = @IsCurrent)

                                                ORDER BY RTrim(StandardsOrgCode) + ' - ' + RTrim(StandardsOrganization);"

                ddlCopyFromSrc.DataValueField = "SlctdSrcID"

                ddlCopyFromSrc.DataTextField = "StndrdOrg"

                lblCopyFromSrc.Text = "Standards Organization"

            End If

     

            ddlCopyFromSrc.Visible = True

            lblCopyFromSrc.Visible = True

     

            CopyFromSrcData.DataBind()

            ddlCopyFromSrc.DataBind()

     

            If rblCstmrOrStndrd.SelectedValue = 1 Then

                ddlCopyFromSrc.Items.Insert(0, New ListItem("- Choose Customer", "-1"))

            ElseIf rblCstmrOrStndrd.SelectedValue = 2 Then

                ddlCopyFromSrc.Items.Insert(0, New ListItem("- Choose Standards Organization", "-1"))

            End If

     

        End Sub

     

        Protected Sub ddlCopyFromSrc_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlCopyFromSrc.SelectedIndexChanged

     

            If rblCstmrOrStndrd.SelectedValue = 1 Then

                SpecToCopyData.SelectCommand = "SELECT DISTINCT Specifications.SpecID, RTrim(Specifications.SpecNmbr)

                                               + ' Rev ' + RTrim(Specifications.SpecRev) AS Spec FROM Specifications

                                               LEFT JOIN SpecToCustomer ON Specifications.SpecID = SpecToCustomer.SpecID

                                               WHERE (SpecToCustomer.CustomerID = @SlctdSrcID)

                                               ORDER BY RTrim(Specifications.SpecNmbr) + ' Rev ' + RTrim(Specifications.SpecRev);"

            ElseIf rblCstmrOrStndrd.SelectedValue = 2 Then

                SpecToCopyData.SelectCommand = "SELECT DISTINCT SpecID, RTrim(SpecNmbr)

                                                + ' Rev ' + RTrim(SpecRev) AS Spec FROM Specifications 

                                                WHERE (StandardsOrgID = @SlctdSrcID)

                                                ORDER BY RTrim(SpecNmbr) + ' Rev ' + RTrim(SpecRev);"

            End If

     

            ddlSpecToCopy.Visible = True

            lblSpecToCopy.Visible = True

     

            SpecToCopyData.DataBind()

            ddlSpecToCopy.DataBind()

     

            ddlSpecToCopy.Items.Insert(0, New ListItem("- Choose Specification", "-1"))

     

        End Sub

     

        Protected Sub ddlSpecToCopy_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecToCopy.SelectedIndexChanged

     

            Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("cnsCoreData")

            Dim cnnCore As New SqlClient.SqlConnection(cts.ConnectionString)

     

            Dim sqlSlctdSpec As String = String.Empty

            Dim cmdSlctdSpec As SqlClient.SqlCommand

            Dim dtrSlctdSpec As SqlClient.SqlDataReader

            Dim lngSpecSrcID As Long = 0

            Dim lngCustomerID As Long = 0

            Dim lngStndrdsOrgID As Long = 0

     

            InitializeLoadSpec()

            SetForNewSpecEntry(False, False)

     

            cnnCore.Open()

     

            lngCopySpecID = ddlSpecToCopy.SelectedValue

     

            sqlSlctdSpec = "SELECT SpecID, CustomerID, StandardsOrgID, SpecNmbr, SpecTitle " _

                                & "FROM Specifications WHERE (((SpecID)=" & lngCopySpecID & "));"

     

            cmdSlctdSpec = New SqlClient.SqlCommand(sqlSlctdSpec, cnnCore)

            dtrSlctdSpec = cmdSlctdSpec.ExecuteReader

     

            divActivityInfo.Visible = False

            btnSaveSpec.Visible = True

     

            lblSpecTitle.Visible = True

            txtSpecTitle.Visible = True

            rblSourceType.Visible = True

     

            If dtrSlctdSpec.HasRows Then

                While dtrSlctdSpec.Read

                    lngCustomerID = dtrSlctdSpec("CustomerID")

                    lngStndrdsOrgID = dtrSlctdSpec("StandardsOrgID")

                    txtSpecTitle.Text = dtrSlctdSpec("SpecTitle")

                    txtSpecTitle_TextChanged(txtSpecTitle, New EventArgs())

                    strCopySpecNmbr = dtrSlctdSpec("SpecNmbr")

                    If lngCustomerID > 0 Then

                        lngSpecSrcID = lngCustomerID

                        If lngCustomerID = 12 Then

                            rblSourceType.SelectedValue = 3

                        Else

                            rblSourceType.SelectedValue = 1

                        End If

                    ElseIf lngStndrdsOrgID > 0 Then

                        lngSpecSrcID = lngStndrdsOrgID

                        rblSourceType.SelectedValue = 2

                    End If

                End While

            End If

     

            rblSourceType_SelectedIndexChanged(sender:=Nothing, e:=Nothing)

     

        End Sub

     

        Protected Sub rblSourceType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblSourceType.SelectedIndexChanged

     

            Try

     

                ddlSpecSource.Items.Clear()

                ddlSpecSource.ClearSelection()

     

                If rblSourceType.SelectedValue = 1 Then

                    SpecSourceData.SelectCommand = "SELECT CustomerID, Customer FROM LUCustomers WHERE (IsCurrent = @IsCurrent) ORDER BY Customer"

                    ddlSpecSource.DataValueField = "CustomerID"

                    ddlSpecSource.DataTextField = "Customer"

                ElseIf rblSourceType.SelectedValue = 2 Then

                    SpecSourceData.SelectCommand = "SELECT StandardsOrgID, RTrim(StandardsOrgCode) + ' - ' + RTrim(StandardsOrganization)

    AS StndrdsOrg FROM LUStandardsOrganizations ORDER BY RTrim(StandardsOrgCode) + ' - ' + RTrim(StandardsOrganization)"

                    ddlSpecSource.DataValueField = "StandardsOrgID"

                    ddlSpecSource.DataTextField = "StndrdsOrg"

                ElseIf rblSourceType.SelectedValue = 3 Then

                    SpecSourceData.SelectCommand = "SELECT CustomerID, Customer FROM LUCustomers WHERE (IsCurrent = @IsCurrent) ORDER BY Customer"

                    ddlSpecSource.DataValueField = "CustomerID"

                    ddlSpecSource.DataTextField = "Customer"

                End If

     

                ddlSpecSource.Visible = True

                If lngCopySpecID > 0 Then

                    lngCopySrcID = ddlCopyFromSrc.SelectedValue

                SpecSourceData.DataBind()

                ddlSpecSource.DataBind()

     

                If rblSourceType.SelectedValue = 1 Then

                    If lngCopySpecID > 0 Then

                        ddlSpecSource.SelectedIndex = ddlSpecSource.Items.IndexOf(ddlSpecSource.Items.FindByValue(lngCopySrcID))

                        ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                    Else

                        ddlSpecSource.Items.Insert(0, New ListItem("- Choose Customer", "-1"))

                    End If

                ElseIf rblSourceType.SelectedValue = 2 Then

                    If lngCopySpecID > 0 Then

                        ddlSpecSource.SelectedIndex = ddlSpecSource.Items.IndexOf(ddlSpecSource.Items.FindByValue(lngCopySrcID))

                        ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                    Else

                        ddlSpecSource.Items.Insert(0, New ListItem("- Choose Standards Organization", "-1"))

                    End If

                ElseIf rblSourceType.SelectedValue = 3 Then

                    ddlSpecSource.SelectedValue = 12

                        ddlSpecSource_SelectedIndexChanged(ddlSpecSource, New EventArgs())

                    End If

     

            Catch ex As Exception

                Debug.Print("Error - LoadSpec.aspx.vb - rblSourceType_SelectedIndexChanged " & Chr(13) _

                            & "Msg = " & ex.Message.ToString & ". Trace:  " & ex.StackTrace)

            End Try

     

        End Sub

     

        Protected Sub ddlSpecSource_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSpecSource.SelectedIndexChanged

     

            Dim strCustomer As String

            Dim strStndrdsOrg As String

     

            If rblSourceType.SelectedValue = 1 Then

                Session("ssnCustomerID") = ddlSpecSource.SelectedValue

                strCustomer = ddlSpecSource.SelectedItem.Text

                Session("ssnStndrdsOrgID") = 0

            ElseIf rblSourceType.SelectedValue = 2 Then

                Session("ssnStndrdsOrgID") = ddlSpecSource.SelectedValue

                strStndrdsOrg = ddlSpecSource.SelectedItem.Text

                Session("ssnCustomerID") = 0

            End If

     

            If lngCopySpecID > 0 Then

                rblCstmrOrStndrd.Visible = False

                lblCstmrOrStndrd.Visible = False

                lblCopyFromSrc.Visible = False

                ddlCopyFromSrc.Visible = False

                lblSpecToCopy.Visible = False

                ddlSpecToCopy.Visible = False

            End If

     

            If rblSourceType.SelectedValue = 1 Then

                SetupAutoDivisions(0)

            End If

     

            lblSpecNmbr.Visible = True

            txtSpecNmbr.Visible = True

            lblRevNmbr.Visible = True

            txtRevNmbr.Visible = True

     

            If lngCopySpecID > 0 Then

                txtSpecNmbr.Text = strCopySpecNmbr

            Else

                txtSpecNmbr.Text = ""

            End If

            txtRevNmbr.Text = ""

            txtRevNmbr.Focus()

     

        End Sub

     

    End Class

    Thursday, May 10, 2018 4:53 AM
  • User347808464 posted

    I've changed the code to not have to use code behind to fire a DropDownList where the items in the list were also provided from code behind.

    I haven't found a solution to the issue itself but I have worked out a different solution for my app and it works fine with the new process.

    Thanks again for the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 11, 2018 6:41 PM