c# - C# 有一个无效的 SelectedIndex,因为它不存在于项目列表中
问题描述
选择大洲,国家后,说明其工作正常。当我再次选择选择大陆时,它会引发错误。我已经创建了tblcontinents
, tblcountries
, , 我在和,和tblstates
之间保持了外键关系。并创建了存储过程。tblcontient
tblcountry
tblcountry
states
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);
}
}
}
解决方案
当您选择不同的大陆时,此if
语句:
if (dropdownlist1.SelectedIndex == 0)
{
...
}
不会被调用(因为新选择的大陆没有索引 0)。这意味着当您更改大陆时,国家和州的下拉菜单不会被重置。
解决此问题的一种简单方法是删除该if
语句,并在更改大陆时始终重置国家和州。更改国家/地区时重置状态也是如此。
推荐阅读
- c++ - 不能在 `std::generate` 中使用 lambda。我哪里搞砸了?
- python-3.x - 使用 sympy 和 PythonTex 更改分数的打印格式
- ruby-on-rails - 模型的验证测试在 rspec 中不起作用
- svg - Gnuplot svg:更改数据点颜色
- javascript - 仅在拖动项目时禁用指针事件
- device - Vulkan 多个逻辑设备来自一个物理设备的后果
- wpf - 通过给定的设备物理特性在 XAML 中设计窗口
- java - 操作超时。春季批次
- django - 使用 Django 创建搜索栏以查询 MongoDB 数据库
- python - 散景中的乳胶(数学模式)