新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮
3.新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮。
省分:<asp:DropDownList ID="DropDownList1" runat="server"
Xonselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
p>
</p>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click"
Text="获取页面中的label 为其赋值" />
在Demo1.aspx页面中引用 ProvincCity控件。并且加入button1和Lable1控件。
<uc1:PCselect ID="PCselect1" runat="server" />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click"
Text="以上选择的城市是:" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
当点击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
<div>
<uc1:PCselect ID="PCselect1" runat="server" />
</div>
。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);
}