用户控件中访问页面中的控件有两种方法:
1 在用户控件中使用parent属性找到页面,在通过findcontrol方法找到要访问的控件
DropDownList ddl= this.city1.FindControl("DropDownList1")as DropDownList;
if (ddl != null)
this.Label1.Text = "省份:" + ddl.SelectedItem.Text + "(" + ddl.SelectedValue + ")";
ddl = this.city1.FindControl("DropDownList2") as DropDownList;
if (ddl != null)
this.Label1.Text += "城市" + ddl.SelectedItem.Text + "(" + ddl.SelectedValue + ")";
2 为用户控件添加事件,在页面中处理事件,进而实现在控件中访问页面中的控件
public delegate void CitySelectHandler(object sender,string selectCity);
public event CitySelectHandler OnCitySelect;
public string ProvinceCity
{
get
{
return this.DropDownList1.SelectedItem.Text + "(" + this.DropDownList1.SelectedValue + ")" + this.DropDownList2.SelectedItem.Text + "(" + this.DropDownList2.SelectedValue + ")";
}
}
。。。。