主页
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="citySelect.ascx.cs" Inherits="shengshi.citySelect1" %>
省份:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="获取页面中的label的值并为其赋值"
οnclick="Button1_Click" />
后台代码
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.Data;
namespace shengshi
{
public partial class citySelect1 : System.Web.UI.UserControl
{
public event CitySelectHandler GetCitySelect;//根据委托定义了一个对应类型的事件
public string GetProvinceCity
{
get { return "省份:"+this.DropDownList1.SelectedItem.Text +" 城市:"+ this.DropDownList2.SelectedItem.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindProvince();
}
}
private void bindProvince()
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select provinceID,province from province";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
con.Open();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.DropDownList2.Items.Clear();
string Pid=this.DropDownList1.SelectedValue;
string sql = "select cityID,city from city where father=@pid";
SqlParameter pms = new SqlParameter("@pid", Pid);
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.Add(pms);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
con.Open();
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "cityID";
DropDownList2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Label lbl= this.Parent.FindControl("label1") as Label;
//lbl.Text = this.DropDownList2.SelectedItem.Text;
GetCitySelect(this, this.DropDownList2.SelectedItem.Text);
if (GetCitySelect != null)
{
GetCitySelect(this, this.DropDownList2.SelectedItem.Text);
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity);//添加一个委托
}
子页
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo1.aspx.cs" Inherits="shengshi.Demo1" %>
<%@ Register src="citySelect.ascx" tagname="citySelect" tagprefix="uc1" %>
<!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>
<uc1:citySelect ID="citySelect1" runat="server" />
<br />
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click"
Text="选中以上城市的名字:" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace shengshi
{
public partial class Demo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.citySelect1.GetCitySelect+=new CitySelectHandler(citySelect1_GetCitySelect);
//增加用户控件CitySelect.aspx事件处理程序
}
void citySelect1_GetCitySelect(object sender, string selectCity)
{
this.Label1.Text=selectCity;
this.TextBox1.Text = selectCity;
}
protected void Button1_Click(object sender, EventArgs e)
{
#region 方法一:FindControl()方法
//DropDownList ddl1=this.citySelect1.FindControl("DropDownList1") as DropDownList;
//this.Label1.Text="省份:"+ ddl1.SelectedItem.Text+" ";
//DropDownList ddl2 = this.citySelect1.FindControl("DropDownList2") as DropDownList;
//this.Label1.Text += "城市:" + ddl2.SelectedItem.Text;
#endregion
#region 方法二:在用户控件中定义属性,这里调用属性获取省市
this.Label1.Text = this.citySelect1.GetProvinceCity;
#endregion
}
//public CitySelectHandler citySelect1_GetCitySelect { get; set; }
}
}