This is a migrated thread and some comments may be shown as answers.

UserControl Drop Down Works some times

2 Answers 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 23 Mar 2016, 01:34 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 28 Mar 2016, 08:46 AM
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.
0
Perry
Top achievements
Rank 1
answered on 28 Mar 2016, 11:48 AM

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>

Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Perry
Top achievements
Rank 1
Share this question
or