简体   繁体   English

来自c#的下拉菜单中的ArgumentOutOfRangeException

[英]ArgumentOutOfRangeException in dropdown from c#

I have two dropdown lists one static( Requested ) another one is dynamic( Response )." Response " is depends on the " Requested " dropdown list.I am new to dot new framework.I don't no I go a exception in my code. 我有两个下拉列表,一个是静态的( Requested ),另一个是动态的( Response )。“ Response ”取决于“ Requested ”下拉列表。我是新加入新框架的人。码。

Ord.aspx Ord.aspx

     <%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="o1.aspx.cs" Inherits="C.Portal.ord" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <div id="Order">
<table width="100%" border="0">

            <tr>
                        <td><asp:Label ID="Label0" runat="server" Text="Requested:* " /></td>           
                       <td>
                            <asp:RequiredFieldValidator ID="v1" ControlToValidate="ddlreq1" CssClass="error" InitialValue="" Display="Dynamic" SetFocusOnError="true" runat="server">
                                Please select request<br />
                            </asp:RequiredFieldValidator>

                            <asp:DropDownList ID="ddlreq1" Width="100%" 
                                TabIndex="15" AutoPostBack="true" 
                                onselectedindexchanged="ddlRequest_SelectedIndexChanged" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>Req1</asp:ListItem>
                                <asp:ListItem>Req2</asp:ListItem>                      
                            </asp:DropDownList>
                        </td>
                    </tr>

                     <tr>
                        <td><asp:Label ID="Label1" runat="server" Text="Response :* " /></td>
                        <td>
                            <asp:RequiredFieldValidator ID="v2" ControlToValidate="ddlresp" CssClass="error" InitialValue="" Display="Dynamic" SetFocusOnError="true" runat="server">
                                Please select a color<br />
                            </asp:RequiredFieldValidator>                    
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>                
                                    <asp:DropDownList ID="ddlresp" DataTextField="ddlText" Width="100%" TabIndex="16" runat="server" />
                                 </ContentTemplate>
                                <Triggers> 
                                    <asp:AsyncPostBackTrigger ControlID="ddlreq1" EventName="SelectedIndexChanged" />
                                </Triggers>  
                             </asp:UpdatePanel>                    

                        </td>
                    </tr>

        </div>
</table>
    </asp:Content>

Ord.aspx.cs Ord.aspx.cs

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Web.Security;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Net.Mail;

    namespace C.Portal
    {
        public partial class ord : System.Web.UI.Page
        {
             protected void Page_Load(object sender, EventArgs e)
            { 
            // other code 
            if (!IsPostBack)
                {
                DataSet Color = new DataSet();
             Color.ReadXml(MapPath("Project/Color.xml"));
             ddlresp.DataSource = Color;
                  ddlresp.DataBind();
                }

            }
            protected void ddlRequest_SelectedIndexChanged(object sender, EventArgs e)
            {
             if (ddlRequest.SelectedValue != "")
              {
                var requestMatrix = (from f in db.RequestMatrixes
                                       where f.Type == ddlRequest.SelectedValue
                                       select f).SingleOrDefault();
                 if (!requestMatrix.testcolor)
                    {
                        ddlresp.SelectedValue = "None";
                        ddlresp.Enabled = false;
                    }
                    else
                    {
                        ddlresp.SelectedIndex = 0; // here I got a exception 
                        ddlresp.Enabled = true;
                    }
                }
            }

        }
    }

I my code exception details: Response dropdown list not loaded.HOw to load the response dropdown list? 我在我的代码异常详细信息: 响应下拉列表不loaded.HOw加载响应下拉列表? some help me. 一些帮助我。

ddlresp' has a SelectedIndex which is invalid because it does not exist in the list of items. ddlresp'的SelectedIndex无效,因为它不存在于项目列表中。 Parameter name: value 参数名称:值

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.ArgumentOutOfRangeException: 'ddlresp' has a SelectedIndex which is invalid because it does not exist in the list of items. 异常详细信息:System.ArgumentOutOfRangeException:'ddlresp'的SelectedIndex无效,因为它不存在于项目列表中。 Parameter name: value 参数名称:值

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 Information regarding the origin and location of the exception can be identified using the exception stack trace below. 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

Stack Trace: 堆栈跟踪:

[ArgumentOutOfRangeException: 'ddlresp' has a SelectedIndex which is invalid because it does not exist in the list of items. [ArgumentOutOfRangeException:'ddlresp'的SelectedIndex无效,因为它不存在于项目列表中。 Parameter name: value] 参数名称:值]

You need to bind the ddlresp after assigning DataSource using the DataBind() method. 使用DataBind()方法分配DataSource之后,需要绑定ddlresp If you do not call DataBind the dropdown wont have any items in it and setting SelectedIndex will throw exception 如果不调用DataBind则下拉列表中将没有任何项目,设置SelectedIndex会引发异常

You may also put it in !Page.IsPostBack block if you need it state. 如果需要状态,也可以将其放在!Page.IsPostBack块中。

protected void Page_Load(object sender, EventArgs e)
{ 
   if(!Page.IsPostBack)
   {
     // other code 
     DataSet Color = new DataSet();
     Color.ReadXml(MapPath("Project/Color.xml"));
     ddlresp.DataSource = Color;
     ddlresp.DataBind();
   }
}

Edit Your html seems invalid as well, you have tr with table tag. 编辑您的html似乎也无效,您使用带有表标签的tr。

Put both dropdowns in UpdatePanel, you may put your table in UpdatePanel. 将两个下拉菜单都放在UpdatePanel中,可以将表放在UpdatePanel中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM