创建数据库表 province 和 city ,使用 dropdownlist 的绑定,实现省市列表级联。

aspx:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            Xonselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
    </div>
    </form>
 
后台

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

namespace 简单绑定
{
    public partial class 省市级联数据绑定 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlDataReader reader = sqlhelper.datareader("select * from province");
                if (reader.HasRows)
                {
                   //读取表中的省份
                    DropDownList1.DataSource = reader;
                  //将表中的省份名称绑定到控件的字段名上
                    DropDownList1.DataTextField = "province";
                    DropDownList1.DataValueField = "id";
                  //将表中的id绑定到控件的value上
                    DropDownList1.DataBind();       
                    DropDownList1.SelectedIndex = 0;
                    //以下是初始化城市列表用的
                    string value = DropDownList1.SelectedValue;
                    SqlDataReader reader2 = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
                    if (reader2.HasRows)
                    {
                        DropDownList2.DataSource = reader2;
                        DropDownList2.DataTextField = "city";
                        DropDownList2.DataValueField = "id";
                        DropDownList2.DataBind();
                    }
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value=DropDownList1.SelectedValue;
            SqlDataReader reader = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
            if (reader.HasRows)
            {
                DropDownList2.DataSource = reader;
                DropDownList2.DataTextField = "city";
                DropDownList2.DataValueField = "id";
                DropDownList2.DataBind();
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要为dropdownlist绑定数据库数据,可以按照以下步骤操作: 1. 在页面添加一个dropdownlist控件。 2. 在页面的代码-behind编写代码来连接数据库并查询需要绑定数据。比如使用ADO.NET的SqlConnection和SqlCommand类来连接数据库并执行查询操作。 3. 将查询结果绑定dropdownlist控件上。可以使用dropdownlist控件的DataSource属性来指定数据源,使用DataTextField和DataValueField属性来指定要显示的文本和值的字段名,最后调用DataBind方法来完成绑定。 以下是一个简单的示例代码: ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connectionString = "Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password"; string query = "SELECT id, name FROM your_table"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "your_table"); dropdownlist.DataSource = dataSet.Tables["your_table"]; dropdownlist.DataTextField = "name"; dropdownlist.DataValueField = "id"; dropdownlist.DataBind(); } } ``` 在这个示例,我们从名为your_table的查询了id和name两个字段的数据,并将其绑定到了名为dropdownlistdropdownlist控件上。其数据库连接字符串、查询语句、数据源字段名和值字段名需要根据实际情况进行修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值