前台如何添加用户控件!
例:
Page指令下加入:
<%@ Register src="citySelect.ascx" tagname="citySelect" tagprefix="uc1" %>
在指定位置下加入:
<uc1:citySelect ID="citySelect1" runat="server" />
页面上访问用户控件中的数据
方法一:
使用用户控件的FindControl方法,找到用户控件中的控件,进一步取得其数据(不需要在控件中处理,但是需要知道用户控件中要使用的控件的ID)
例:DropDownList dd1 = this.citySelect1.FindControl("DropDownList1") as DropDownList;
方法二:
为控件增加属性(方法),使用该属性访问用户控件中的数据(在目标页面中使用时,简单,但是不够灵活)
例(用户控件CS):
public string ProvinceCity
{
get
{
return this.DropDownList1.SelectedItem.Text + "(" + this.DropDownList1.SelectedValue + ");" + this.DropDownList2.SelectedItem.Text + "(" + this.DropDownList2.SelectedValue + ");";
}
}
{
get
{
return this.DropDownList1.SelectedItem.Text + "(" + this.DropDownList1.SelectedValue + ");" + this.DropDownList2.SelectedItem.Text + "(" + this.DropDownList2.SelectedValue + ");";
}
}
用户控件中访问页面中的控件
方法一:
在用户控件中使用Parent属性找到页面,再通过FindControl方法找到要访问的控件
例:Label lb = this.Parent.FindControl("Label1") as Label;
方法二:
为用户控件添加事件,在页面中处理事件,进而实现在控件中访问页面中的控件
定义委托,处理事件!实现步骤就不在这里介绍了!网上找一下吧1