新建 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>
&nbsp;</p>
<asp:Button ID="Button1" runat="server" Xοnclick="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" Xοnclick="Button1_Click"
Text="以上选择的城市是:" />
&nbsp;
<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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值