Asp.net中用户自定义控件 ascx的使用

本文介绍如何在ASP.NET中使用用户控件(ascx)实现省市区三级联动,并详细解释了参数的输入和输出过程。通过自定义属性传递参数,并通过方法获取输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用ascx目的就是为了提高某部分功能的重复利用,我简单通过源代码说一下对它的参数的输入和数出。

我们以省市区三级连动为例子。

vs2005下ascx页面的代码:

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><tablewidth="100%"border="0"cellpadding="0"cellspacing="0">
<tr>
<td>
<asp:DropDownListid="ddlProvince"runat="server"Width="100px"AutoPostBack="True"OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">
</asp:DropDownList>
<asp:UpdatePanelID="UpdatePanel1"runat="server"RenderMode="Inline">
<ContentTemplate>
<asp:DropDownListid="ddlCity"runat="server"Width="100px"AutoPostBack="True"OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTriggerControlID="ddlProvince"EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanelID="UpdatePanel2"runat="server"RenderMode="Inline">
<ContentTemplate>
<asp:DropDownListid="ddlDistrict"runat="server"Width="100px">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTriggerControlID="ddlCity"EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>

ascx后台代码:

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicpartialclassUserControl_ProvinceAndCityAndDistrict:System.Web.UI.UserControl
{
/////<summary>
/////选择区域的iD
/////</summary>
protectedstring_districtValue;
[Bindable(
true),Category("Appearance"),DefaultValue("")]
publicstringdistrictValue
{
get{return_districtValue;}
set{_districtValue=value;}
}

protectedstring_cityValue;
[Bindable(
true),Category("Appearance"),DefaultValue("")]
publicstringcityValue
{
get{return_cityValue;}
set{_cityValue=value;}
}

protectedstring_provinceValue;
[Bindable(
true),Category("Appearance"),DefaultValue("")]
publicstringprovinceValue
{
get{return_provinceValue;}
set{_provinceValue=value;}
}

privatevoidsetValue()
{
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;
}
}
publicstringgetValue()
{
returnthis.ddlProvince.SelectedItem.Text+","+ddlCity.SelectedItem.Text+","+ddlDistrict.SelectedItem.Text;
}

protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
setProvince();
this.ddlProvince.SelectedIndex=0;
setCity(Convert.ToInt32(
this.ddlProvince.SelectedValue));
this.ddlCity.SelectedIndex=0;
setDistrict(Convert.ToInt32(
this.ddlCity.SelectedValue));
setValue();
}
}
privatevoidsetProvince()
{
CallCenter.BLL.sys_Provinceprovince
=newCallCenter.BLL.sys_Province();
DataSetds
=province.getProvinceList();
this.ddlProvince.DataSource=ds.Tables[0];
this.ddlProvince.DataValueField="provinceid";
this.ddlProvince.DataTextField="provincename";
this.ddlProvince.DataBind();
}
privatevoidsetCity(intprovinceId)
{
CallCenter.BLL.sys_Citycity
=newCallCenter.BLL.sys_City();
DataSetds
=city.getCityList(provinceId);
this.ddlCity.DataSource=ds.Tables[0];
this.ddlCity.DataValueField="cityid";
this.ddlCity.DataTextField="cityName";
this.ddlCity.DataBind();
}
privatevoidsetDistrict(intcityId)
{
CallCenter.BLL.sys_Districtdistrict
=newCallCenter.BLL.sys_District();
DataSetds
=district.getDistrictList(cityId);
this.ddlDistrict.DataSource=ds.Tables[0];
this.ddlDistrict.DataValueField="districtid";
this.ddlDistrict.DataTextField="districtname";
this.ddlDistrict.DataBind();
}
protectedvoidddlProvince_SelectedIndexChanged(objectsender,EventArgse)
{
setCity(Convert.ToInt32(
this.ddlProvince.SelectedValue));
this.ddlCity.SelectedIndex=0;
setDistrict(Convert.ToInt32(
this.ddlCity.SelectedValue));
}
protectedvoidddlCity_SelectedIndexChanged(objectsender,EventArgse)
{
setDistrict(Convert.ToInt32(
this.ddlCity.SelectedValue));
}
}

使用aspx页面的后台调用部分代码:

如何传入参数,对于ascx的参数一般通过自定义页面属性来实现的。在ascx的页面我定义了几个属性

provincevalue,cityvalue,districtvalue,把你所需要传入的参数通过下面的方式给它就可以了,至于出入后你需要怎么处理你就自己操作了,上面有我的简单处理方式。

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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的使用有个初步简单的了解。有好的方法相互交流,共同进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值