UserControl Drop Down Works some times

3 posts, 1 answers
  1. Perry
    Perry avatar
    25 posts
    Member since:
    Jul 2015

    Posted 23 Mar Link to this post

    I have a very strange problem I am using a RadGrid with an .ascx user control form to do editing and adding of new records.  The form works fine when I am editing a record but crashes when I attempt to add a record.

    This is the code used to setup the dropdown on the .ascx page

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource1"
                            DataTextField="StatusDescription" DataValueField="StatusDescription"
                            SelectedValue='<%# DataBinder.Eval(Container, "DataItem.Status") %>'>
                            <asp:ListItem Selected="True" Text="Select" Value="">
                                </asp:ListItem>
                        </asp:DropDownList>

    This is the C# code behind for that page.

    namespace CrimeTips
    {
        public partial class InvestigationDetailsControl : System.Web.UI.UserControl
        {
            private object _dataItem = null;
     
     
            public object DataItem
            {
                get
                {
                    return this._dataItem;
                }
                set
                {
                    this._dataItem = value;
                }
     
            }
     
            protected void DropDownList1_SelectedIndexChanged(object sender, DropDownListEventArgs e)
            {
                string StatusValue = DropDownList1.DataValueField;
            }
             
        }
    }

    And here is the ASPX code for the EditFormSettings

    <EditFormSettings UserControlName="InvestigationDetailsControl.ascx" EditFormType="WebUserControl">
                            <EditColumn UniqueName="EditCommandColumn1">
     
                        </EditColumn>
                        </EditFormSettings>

    The problem is when I edit the data the form works without any issues.  However, when I attempt to add a records the application crashes with the yellow screen of death with an error message stating

    'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.          
    Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    I do not understand why the form works for editing purposes but will not render for the add record can someone offer an explanation and a fix for the problem?

     

    Thanks

  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 28 Mar Link to this post

    Hi Perry,

    In order to avoid the error you can specify the DefaultInsertValue property for the GridTemplateColumn. This way the default value will be used initially for the DropDownList control.


    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Perry
    Perry avatar
    25 posts
    Member since:
    Jul 2015

    Posted 28 Mar in reply to Viktor Tachev Link to this post

    I found the error and the drop down is now working as expected.  I had to mofify the asp properties the corrected code is shown below.

     

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource1"
                            DataTextField="StatusDescription" DataValueField="StatusDescription" AppendDataBoundItems ="true"
                            SelectedValue='<%# DataBinder.Eval(Container, "DataItem.Status") %>'>
                            <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
                        </asp:DropDownList>
                        </td>

Back to Top