省时选择器

本文详细介绍了ASP.NET网页中使用下拉列表进行省份和城市选择,并通过C#代码实现与数据库的交互,获取并填充列表项的过程。包括页面初始化、事件响应、数据库连接、查询操作及数据绑定等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

aspx事件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="省时选择器.aspx.cs" Inherits="_922练习.省时选择器" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DropDownList ID="DropDownList1" runat="server" Height="29px"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="150px">
            <asp:ListItem Value="0">----请选择省份----</asp:ListItem>
        </asp:DropDownList>
&nbsp;
        <asp:DropDownList ID="DropDownList2" runat="server" Height="29px" Width="135px">
            <asp:ListItem Value="0">----请选择市----</asp:ListItem>
        </asp:DropDownList>
   
    </div>
    </form>
</body>
</html>

 

aspx.cs事件

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;

namespace _922练习
{
    public partial class 省时选择器 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.DropDownList1.Items.Add(new ListItem("----请选择省份----", "0"));
                FillDownList();

            }
        }
        private void FillDownList()
        {

            string connstr = "Data Source=WIN-10AFVI27V7T;Initial Catalog=921练习;Persist Security Info=True;User ID=sa;Password=admin";

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from Table_1";

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        #region
                        //this.DropDownList1.DataSource = reader;
                        //this.DropDownList1.DataTextField = "Pname";
                        //this.DropDownList1.DataValueField = "id";

                        //this.DropDownList1.DataBind();

                        #endregion

                        #region  old method
                        ListItem li;
                        while (reader.Read())
                        {
                            string name = reader.GetString(reader.GetOrdinal("Pname"));
                            int id = reader.GetInt32(reader.GetOrdinal("Id"));
                            li = new ListItem(name, id.ToString());
                            this.DropDownList1.Items.Add(li);

                        }
                        #endregion

                    }
                }

            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string connstr = "Data Source=WIN-10AFVI27V7T;Initial Catalog=921练习;Persist Security Info=True;User ID=sa;Password=admin";

 

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select id,Fname from Table_2 where Fid=@xxx";

                    SqlParameter paran = new SqlParameter("xxx", this.DropDownList1.SelectedValue);
                    cmd.Parameters.Add(paran);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.DropDownList2.DataSource = reader;
                     
                        this.DropDownList2.DataTextField = "Fname";
                        this.DropDownList2.DataValueField = "id";
                        this.DropDownList2.DataBind();
                    }
                }
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值