1

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.

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

 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? some help me.

ddlresp' has a SelectedIndex 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: 'ddlresp' has a SelectedIndex which is invalid because it does not exist in the list of items. Parameter name: value

Source Error:

An unhandled exception was generated during the execution of the current web request. 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. Parameter name: value]

3
  • If you remove that line, do you see any data in ddlresp? If there are no items, you can't select the first one... Commented May 30, 2014 at 5:49
  • 1
    I think problem is this ddlPad.DataBind();. he is not binding ddlresp Commented May 30, 2014 at 5:55
  • Your html seems invalid as well, you have tr with table tag Commented May 30, 2014 at 6:00

1 Answer 1

1

You need to bind the ddlresp after assigning DataSource using the DataBind() method. If you do not call DataBind the dropdown wont have any items in it and setting SelectedIndex will throw exception

You may also put it in !Page.IsPostBack block if you need it state.

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.

Put both dropdowns in UpdatePanel, you may put your table in UpdatePanel.

Sign up to request clarification or add additional context in comments.

2 Comments

I think it should be also inside !isPostback?
Thanks, I agree with you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.