首页 > 解决方案 > C# 有一个无效的 SelectedIndex,因为它不存在于项目列表中

问题描述

选择大洲,国家后,说明其工作正常。当我再次选择选择大陆时,它会引发错误。我已经创建了tblcontinents, tblcountries, , 我在和,和tblstates之间保持了外键关系。并创建了存储过程。tblcontienttblcountrytblcountrystates

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

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               DataSet ds= getname("spgetcontinentbyid", null);
               dropdownlist1.DataTextField = "ContinentName";
               dropdownlist1.DataValueField = "id";
               dropdownlist1.DataSource = ds;
               dropdownlist1.DataBind();

               ListItem li1 = new ListItem("Select Continent", "-1");

               dropdownlist1.Items.Insert(0,li1);

               ListItem li2 = new ListItem("Select Country", "-1");

               dropdownlist2.Items.Insert(0, li2);

               ListItem li3 = new ListItem("Select State", "-1");

               dropdownlist3.Items.Insert(0, li3);

               dropdownlist2.Enabled = false;
               dropdownlist3.Enabled = false;




            }
        }

        private DataSet getname(string spname, SqlParameter sqlparameter)
        {
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            SqlConnection cn = new SqlConnection(cs);
            SqlDataAdapter da = new SqlDataAdapter(spname, cn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            if (sqlparameter != null)
            {

                da.SelectCommand.Parameters.Add(sqlparameter);


            }


            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;




        }

        protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropdownlist1.SelectedIndex == 0)
            {

                dropdownlist2.SelectedIndex = 0;
                dropdownlist2.Enabled = false;

                dropdownlist3.SelectedIndex = 0;
                dropdownlist3.Enabled = false;

            }
            dropdownlist2.Enabled = true;

            SqlParameter param1 = new SqlParameter("@contintentid", dropdownlist1.SelectedValue);


             dropdownlist2.DataSource=getname("spgetcountrybyid",param1);
             //dropdownlist2.DataTextField = "CountryName";
             //dropdownlist2.DataValueField = "id";

            dropdownlist2.DataBind();

            ListItem li = new ListItem("select Country", "-1");
            dropdownlist2.Items.Insert(0,li);

            //dropdownlist2.SelectedIndex = 0;
            //dropdownlist2.Enabled = false;

            dropdownlist3.SelectedIndex = 0;
            dropdownlist3.Enabled = false;
        }

        protected void dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropdownlist2.SelectedIndex == 0)
            {
                dropdownlist3.SelectedIndex = 0;
                dropdownlist3.Enabled = false;
            }
            dropdownlist3.Enabled = true;
            SqlParameter param2 = new SqlParameter("@countryid", dropdownlist2.SelectedValue);
            DataSet ds = getname("spgetstatesbycountryid", param2);
            dropdownlist3.DataTextField = "Statename";
            dropdownlist3.DataValueField = "id";
            dropdownlist3.DataSource = ds;
            dropdownlist3.DataBind();

            ListItem li = new ListItem("select City", "-1");
            dropdownlist3.Items.Insert(0, li);

        }

    }
}

标签: c#

解决方案


当您选择不同的大陆时,此if语句:

if (dropdownlist1.SelectedIndex == 0)
{
    ...
}

不会被调用(因为新选择的大陆没有索引 0)。这意味着当您更改大陆时,国家和州的下拉菜单不会被重置。

解决此问题的一种简单方法是删除该if语句,并在更改大陆时始终重置国家和州。更改国家/地区时重置状态也是如此。


推荐阅读