使用另一个下拉列表填充下拉列表 [英] Populate a dropdownlist using another dropdownlist

查看:79
本文介绍了使用另一个下拉列表填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想知道当你在另一个与数据库相关的下拉列表中选择一个值时如何填写一个下拉列表,我知道它很常见但是我不熟悉它中的函数Asp.net。



提前谢谢



我尝试了什么:



您好,

我想知道如何在与数据库相关的另一个下拉列表中选择一个值时填充下拉列表,我知道这很平常,但我不熟悉Asp.net中的功能。



提前谢谢

解决方案

您需要的是级联下拉列表。以下是我在网上发现的一些文章:



ASP.NET WebForms:在ASP.Net中创建级联DropDownLists

ASP.NET与jQuery:使用jQuery和ASP.NET级联DropDownLists

使用AJAXControlToolkit的ASP.NET:使用ASP.Net的数据库的AJAX级联DropDownList示例 />
ASP.NET MVC:在MVC中使用创建级联DropDownList实体框架和ADO.NET



根据上面的链接,有很多方法可以实现级联下拉列表。在ASP.NET WebForms 的上下文中,您可以使用 jQuery / JavaScript AJAX ,使用 AJAXControlToolkit CascadingDropDown 控件c>或在服务器上手动执行此操作。这是使用ADO.NET方式的服务器端快速实现:



  private   string  GetConnectionString()
{
// 调用从Web配置文件设置的连接字符串
return System.Configuration.ConfigurationManager.ConnectionStrings [ DBConnection]。ConnectionString;
}

private void BindDropDownList1()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
尝试
{
connection.Open();
string sqlStatement = SELECT ColumnName * FROM表名;
SqlCommand sqlCmd = new SqlCommand(sqlStatement,connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0
{
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = ColumnName; // 要在列表项中显示的项目
DropDownList1.DataValueField = ColumnName; // 显示项目的ID
DropDownList1.DataBind();
}
}
最后
{
connection.Close();
}
}

private void BindDropDownList2( string 字段)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
尝试
{
connection.Open();
string sqlStatement = SELECT * FROM Table在哪里ColumnName = @ Value1;
SqlCommand sqlCmd = new SqlCommand(sqlStatement,connection);
sqlCmd .Parameters.AddWithValue( @ Value1,field)
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0
{
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = ColumnName; // 要在列表项中显示的项目
DropDownList2.DataValueField = ColumnName; // 显示项目的ID
DropDownList2.DataBind();
}
}
最后
{
connection.Close();
}
}

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList1();
}
}

受保护 void DropDownList1_SelectedIndexChanged( object sender,System.EventArgs e)
{

BindDropDownList2(DropDownList1.SelectedItem.Text);

}





想法是在做出选择时传递所选项目值从下拉列表中使用该值查询数据库并使用相关数据重新填充第二个下拉列表。 / BLOCKQUOTE>

Hello,
I would like to know how to fill a dropdownlist when you choose a value in another dropdownlist related to the database, I know it's commonplace but I'm not familiar with the functions in Asp.net.

thank you in advance

What I have tried:

Hello,
I would like to know how to fill a dropdownlist when you choose a value in another dropdownlist related to the database, I know it's commonplace but I'm not familiar with the functions in Asp.net.

thank you in advance

解决方案

What you need is called "cascading dropdownlist". Here's a few articles I found on the net:

ASP.NET WebForms: Creating Cascading DropDownLists in ASP.Net
ASP.NET with jQuery : Cascading DropDownLists with jQuery and ASP.NET
ASP.NET with AJAXControlToolkit: AJAX Cascading DropDownList sample with database using ASP.Net
ASP.NET MVC: Creating Cascading DropDownList In MVC Using Entity Framework And ADO.NET

Based on the links above, there are many ways to implement cascading dropdownlist. In the context of ASP.NET WebForms, you could either use jQuery/JavaScript AJAX, use the CascadingDropDown control from AJAXControlToolkit or manually doing it at the server. Here's a server-side quick implementation using the ADO.NET way:

 private string GetConnectionString()
 {
    //calling up the connection string that was set up from the web config file
     return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
 }

 private void BindDropDownList1()
 {
     DataTable dt = new DataTable();
     SqlConnection connection = new SqlConnection(GetConnectionString());
     try
     {
         connection.Open();
         string sqlStatement = "SELECT ColumnName * FROM TableName";
         SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
         SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
         sqlDa.Fill(dt);
         if (dt.Rows.Count > 0)
         {
             DropDownList1.DataSource =dt;
             DropDownList1.DataTextField = "ColumnName"; // the items to be displayed in the list items
             DropDownList1.DataValueField = "ColumnName"; // the id of the items displayed
             DropDownList1.DataBind();
         }
     }
     finally
     {
         connection.Close();
     }
 }

 private void BindDropDownList2(string field)
 {
     DataTable dt = new DataTable();
     SqlConnection connection = new SqlConnection(GetConnectionString());
     try
     {
         connection.Open();
         string sqlStatement = "SELECT * FROM Table WHERE ColumnName = @Value1";
         SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
         sqlCmd .Parameters.AddWithValue("@Value1", field)
         SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
         sqlDa.Fill(dt);
         if (dt.Rows.Count > 0)
         {
             DropDownList2.DataSource =dt;
             DropDownList2.DataTextField = "ColumnName"; // the items to be displayed in the list items
             DropDownList2.DataValueField = "ColumnName"; // the id of the items displayed
             DropDownList2.DataBind();
         }
     }
     finally
     {
         connection.Close();
     }
 }

protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
         BindDropDownList1();
     }
 }

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{

   BindDropDownList2(DropDownList1.SelectedItem.Text);

}



The idea is to pass the selected item value when a selection is made from the dropdownlist and use that value to query your database and re-populate your second dropdownlist with the associated data.


这篇关于使用另一个下拉列表填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆