---
title: "新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮"
author: "mywebcode"
date: "2012-12-26T21:35:00Z"
tags: []
slug: "provincecity-button1"
draft: false
lastmod: "2013-05-08T10:26:17Z"
id: 1862844
url: "https://mywebcode.iteye.com/blog/1862844"
---

3.新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮。  
省分：&lt;asp:DropDownList ID="DropDownList1" runat="server"  
Xonselectedindexchanged="DropDownList1\_SelectedIndexChanged"  
AutoPostBack="True"&gt;  
&lt;/asp:DropDownList&gt;  
城市：&lt;asp:DropDownList ID="DropDownList2" runat="server"&gt;  
&lt;/asp:DropDownList&gt;  
p&gt;  
&nbsp;&lt;/p&gt;  
&lt;asp:Button ID="Button1" runat="server" Xonclick="Button1\_Click"  
Text="获取页面中的label 为其赋值" /&gt;  
在Demo1.aspx页面中引用 ProvincCity控件。并且加入button1和Lable1控件。  
&lt;uc1:PCselect ID="PCselect1" runat="server" /&gt;  
&lt;br /&gt;  
&lt;br /&gt;  
&lt;br /&gt;  
&lt;br /&gt;  
&lt;asp:Button ID="Button1" runat="server" Xonclick="Button1\_Click"  
Text="以上选择的城市是：" /&gt;  
&nbsp;  
&lt;asp:Label ID="Label1" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;

当点击Demo1中的button时，在lable1中显示用户在provinceCity控件中选择的省和市。

DropDownList ddl= this.PCselect1.FindControl("DropDownList1") as DropDownList;  
this.Label1.Text="省份："+ ddl.SelectedItem.Text;

DropDownList ddl2 = this.PCselect1.FindControl("DropDownList2") as DropDownList;  
this.Label1.Text += "；城市：" + ddl2.SelectedItem.Text;

使用两种方法，实现当点击用户控件中的button1时，让demo1中的lable显示ProvinceCity控件中 drowpdownList2中选中的内容。  
#region 方法一 findControl（）

// DropDownList ddl= this.PCselect1.FindControl("DropDownList1") as DropDownList;  
//this.Label1.Text="省份："+ ddl.SelectedItem.Text;

////DropDownList ddl2 = this.PCselect1.FindControl("DropDownList2") as DropDownList;  
//this.Label1.Text += "；城市：" + ddl2.SelectedItem.Text;  
#endregion

protected void Button1\_Click(object sender, EventArgs e)  
{  
Label lbl= this.Parent.FindControl("label1") as Label;

lbl.Text = this.DropDownList2.SelectedItem.Text;

}  
#region 方法二 在用户控件中定义属性 这里调用属性  
this.Label1.Text = this.PCselect1.GetProvinceCity;  
#endregion

后台  
public string GetProvinceCity  
{  
get { return "省份："+this.DropDownList1.SelectedItem.Text + "城市："+this.DropDownList2.SelectedItem.Text; }  
}  
4新建 demo2.aspx，引用ProvinceCity，当点击 provinceCity中的button时，在页面中显示选择的省和市。  
demo2.aspx  
&lt;div&gt;

&lt;uc1:PCselect ID="PCselect1" runat="server" /&gt;

&lt;/div&gt;  
。cs  
protected void Page\_Load(object sender, EventArgs e)  
{  
this.PCselect1.GetcitySelect += new CitySelectHander(PCselect1\_GetcitySelect);  
}

void PCselect1\_GetcitySelect(object sender, string selectCity)  
{  
this.Response.Write(selectCity);  
}