Asp.net中用户自定义控件 ascx的使用
使用ascx目的就是为了提高某部分功能的重复利用,我简单通过源代码说一下对它的参数的输入和数出。
我们以省市区三级连动为例子。
vs2005下ascx页面的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ascx后台代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public partial class UserControl_ProvinceAndCityAndDistrict : System.Web.UI.UserControl
{
///// <summary>
///// 选择区域的iD
///// </summary>
protected string _districtValue;
[Bindable(true), Category("Appearance"), DefaultValue("")]
public string districtValue
{
get { return _districtValue; }
set { _districtValue = value; }
}
protected string _cityValue;
[Bindable(true), Category("Appearance"), DefaultValue("")]
public string cityValue
{
get { return _cityValue; }
set { _cityValue = value; }
}
protected string _provinceValue;
[Bindable(true), Category("Appearance"), DefaultValue("")]
public string provinceValue
{
get { return _provinceValue; }
set { _provinceValue = value; }
}
private void setValue()
{
if (_provinceValue != string.Empty)
{
this.ddlProvince.SelectedItem.Text = _provinceValue;
}
if (_cityValue != string.Empty)
{
this.ddlCity.SelectedItem.Text = _cityValue;
}
if (_districtValue != string.Empty)
{
this.ddlDistrict.SelectedItem.Text = _districtValue;
}
}
public string getValue()
{
return this.ddlProvince.SelectedItem.Text + "," + ddlCity.SelectedItem.Text + "," + ddlDistrict.SelectedItem.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setProvince();
this.ddlProvince.SelectedIndex = 0;
setCity(Convert.ToInt32(this.ddlProvince.SelectedValue));
this.ddlCity.SelectedIndex = 0;
setDistrict(Convert.ToInt32(this.ddlCity.SelectedValue));
setValue();
}
}
private void setProvince()
{
CallCenter.BLL.sys_Province province = new CallCenter.BLL.sys_Province();
DataSet ds = province.getProvinceList();
this.ddlProvince.DataSource = ds.Tables[0];
this.ddlProvince.DataValueField = "provinceid";
this.ddlProvince.DataTextField = "provincename";
this.ddlProvince.DataBind();
}
private void setCity(int provinceId)
{
CallCenter.BLL.sys_City city = new CallCenter.BLL.sys_City();
DataSet ds = city.getCityList(provinceId);
this.ddlCity.DataSource = ds.Tables[0];
this.ddlCity.DataValueField = "cityid";
this.ddlCity.DataTextField = "cityName";
this.ddlCity.DataBind();
}
private void setDistrict(int cityId)
{
CallCenter.BLL.sys_District district = new CallCenter.BLL.sys_District();
DataSet ds = district.getDistrictList(cityId);
this.ddlDistrict.DataSource = ds.Tables[0];
this.ddlDistrict.DataValueField = "districtid";
this.ddlDistrict.DataTextField = "districtname";
this.ddlDistrict.DataBind();
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
setCity(Convert.ToInt32(this.ddlProvince.SelectedValue));
this.ddlCity.SelectedIndex = 0;
setDistrict(Convert.ToInt32(this.ddlCity.SelectedValue));
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
setDistrict(Convert.ToInt32(this.ddlCity.SelectedValue));
}
}
使用aspx页面的后台调用部分代码:
如何传入参数,对于ascx的参数一般通过自定义页面属性来实现的。在ascx的页面我定义了几个属性
provincevalue,cityvalue,districtvalue,把你所需要传入的参数通过下面的方式给它就可以了,至于出入后你需要怎么处理你就自己操作了,上面有我的简单处理方式。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
string[] strAddress=ds.Tables[0].Rows[i]["value"].ToString().Split(',');
this.ProvinceAndCityAndDistrict1.provinceValue = strAddress[0];
this.ProvinceAndCityAndDistrict1.cityValue = strAddress[1];
this.ProvinceAndCityAndDistrict1.districtValue = strAddress[2];
从ascx获取输出的东西的话,我目前采用的是通过公共函数调用来实现的
比如在对aspx页面的信息进行保存的时候,我要获取到当前所选择的省市区信息,我是通过方法getValue()来获取返回的字符窜的,你可以根据自己的需要来定义适当的方法。
this.ProvinceAndCityAndDistrict1.getValue();
希望我通过简单的举例说明能够帮助大家对ascx的使用有个初步简单的了解。有好的方法相互交流,共同进步!