C# 带有SQLdatasource的Dropdownlist层次结构
在我的ASP.NET文件夹中,我有3个下拉框,其中填充了3种不同SQLDataSource的类别。每个dropdownlist取决于它上面的一个。C# 带有SQLdatasource的Dropdownlist层次结构,c#,asp.net,sqldatasource,C#,Asp.net,Sqldatasource,在我的ASP.NET文件夹中,我有3个下拉框,其中填充了3种不同SQLDataSource的类别。每个dropdownlist取决于它上面的一个。 这样做的目的是,在DropDownList 1中选择某个内容后,DropDownList 2中的数据源会发生变化。因此,dropdown2中的数据取决于dropdown1中选择的值。dropdown3中的数据取决于dropdown2中选择的值。 第一次一切都很好,很漂亮。但当我在dd1中选择一个值,然后在dd2中选择一个值时,它开始失败。 例如,当我
这样做的目的是,在DropDownList 1中选择某个内容后,DropDownList 2中的数据源会发生变化。因此,dropdown2中的数据取决于dropdown1中选择的值。dropdown3中的数据取决于dropdown2中选择的值。
第一次一切都很好,很漂亮。但当我在dd1中选择一个值,然后在dd2中选择一个值时,它开始失败。
例如,当我在dropdown1中第二次更改该值时,其他DropDownList不会更改
提前回复Thx要做这类事情,你需要确保你的顶级下拉列表只填充一次,所以用一个!我绕着它转。 然后在顶级下拉列表上附加OnSelectedIndexChanged()事件,在设置新数据源之前,请确保在其中清除第二级下拉列表中的项目 然后,在第二级下拉列表上显示OnSelectedIndexChanged(),并确保在填充第三级之前清除它
然后,第三个不需要任何事件。要执行此类操作,您需要确保顶层下拉列表只填充一次,因此请将其放入页面加载中,并添加一个!我绕着它转。 然后在顶级下拉列表上附加OnSelectedIndexChanged()事件,在设置新数据源之前,请确保在其中清除第二级下拉列表中的项目 然后,在第二级下拉列表上显示OnSelectedIndexChanged(),并确保在填充第三级之前清除它
然后第三个不需要任何事件。将要填充的代码
dd2
放入dd1
的OnSelectedIndexChanged
,并对dd2
的OnSelectedIndexChanged
执行相同操作(填充dd3
)
public partial class Default : Page
{
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var datacontext = new DataClasses1DataContext();
List<Company> companies = datacontext.Companies.ToList();
DropDownList1.DataSource = companies;
DropDownList1.DataValueField = "CompanyID";
DropDownList1.DataTextField = "CompanyName";
DropDownList1.DataBind();
}
}
public void DropDownList1SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new DataClasses1DataContext())
{
DetailsView2.DataSource = null;
DetailsView2.DataBind();
DetailsView1.DataSource = dc.Companies.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DetailsView1.DataBind();
List<Contact> contacts =
dc.Contacts.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DropDownList2.DataSource = contacts;
DropDownList2.DataTextField = "LastName";
DropDownList2.DataValueField = "ContactID";
DropDownList2.DataBind();
if (contacts.Any())
{
DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
public void DropDownList2SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new DataClasses1DataContext())
{
DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
公共部分类默认值:第页
{
公共无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
var datacontext=new DataClasses1DataContext();
List companys=datacontext.companys.ToList();
DropDownList1.DataSource=公司;
DropDownList1.DataValueField=“CompanyID”;
DropDownList1.DataTextField=“CompanyName”;
DropDownList1.DataBind();
}
}
public void DropDownList1SelectedIndexChanged(对象发送方,事件参数e)
{
使用(var dc=new DataClasses1DataContext())
{
DetailsView2.DataSource=null;
DetailsView2.DataBind();
DetailsView1.DataSource=dc.companys.Where(d=>d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DetailsView1.DataBind();
列出联系人=
其中(d=>d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DropDownList2.DataSource=联系人;
DropDownList2.DataTextField=“LastName”;
DropDownList2.DataValueField=“ContactID”;
DropDownList2.DataBind();
if(contacts.Any())
{
DetailsView2.DataSource=dc.Contacts.Where(c=>c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
public void DropDownList2SelectedIndexChanged(对象发送方,事件参数e)
{
使用(var dc=new DataClasses1DataContext())
{
DetailsView2.DataSource=dc.Contacts.Where(c=>c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
两个不同表通过键连接的代码示例,在本例中为companyID
public partial class Default : Page
{
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var datacontext = new DataClasses1DataContext();
List<Company> companies = datacontext.Companies.ToList();
DropDownList1.DataSource = companies;
DropDownList1.DataValueField = "CompanyID";
DropDownList1.DataTextField = "CompanyName";
DropDownList1.DataBind();
}
}
public void DropDownList1SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new DataClasses1DataContext())
{
DetailsView2.DataSource = null;
DetailsView2.DataBind();
DetailsView1.DataSource = dc.Companies.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DetailsView1.DataBind();
List<Contact> contacts =
dc.Contacts.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DropDownList2.DataSource = contacts;
DropDownList2.DataTextField = "LastName";
DropDownList2.DataValueField = "ContactID";
DropDownList2.DataBind();
if (contacts.Any())
{
DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
public void DropDownList2SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new DataClasses1DataContext())
{
DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
公共部分类默认值:第页
{
公共无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
var datacontext=new DataClasses1DataContext();
List companys=datacontext.companys.ToList();
DropDownList1.DataSource=公司;
DropDownList1.DataValueField=“CompanyID”;
DropDownList1.DataTextField=“CompanyName”;
DropDownList1.DataBind();
}
}
public void DropDownList1SelectedIndexChanged(对象发送方,事件参数e)
{
使用(var dc=new DataClasses1DataContext())
{
DetailsView2.DataSource=null;
DetailsView2.DataBind();
DetailsView1.DataSource=dc.companys.Where(d=>d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DetailsView1.DataBind();
列出联系人=
其中(d=>d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
DropDownList2.DataSource=联系人;
DropDownList2.DataTextField=“LastName”;
DropDownList2.DataValueField=“ContactID”;
DropDownList2.DataBind();
if(contacts.Any())
{
DetailsView2.DataSource=dc.Contacts.Where(c=>c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
public void DropDownList2SelectedIndexChanged(对象发送方,事件参数e)
{
使用(var dc=new DataClasses1DataContext())
{
DetailsView2.DataSource=dc.Contacts.Where(c=>c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
DetailsView2.DataBind();
}
}
}
thx m8它已经帮了很多忙了^^^现在我