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

摘要:
使用ascx的目的是提高某些函数的重用性。我将通过源代码简单地讨论其参数的输入和输出。让我们以省市区三级联动运动为例。vs2005中ascx页面的代码:codeascx背景代码:CodepublicpartialclassUserControl_ ProvinceAndCityAndDistrict:System.Web.UI。UserControl{/////////选择iD//////protectedstring_districtValue;[Bindable,Category,DefaultValue(“”)]publicstringdistrictValue{get{return_districtValue;}设置{_districtValue=value;}}受保护字符串_城市值;[Bindable,Category,DefaultValue(“”)]publicstringcityValue{get{return_cityValue;}设置{_cityValue=值;}}受保护字符串_ provinceValue;[Bindable,Category,DefaultValue(“”)]publicstringProvisionValue{get{return_proviceValue;}设置{_proviceValue=value;}}privatevoisetValue(){if(_proviceValue!

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

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

vs2005下ascx页面的代码:

Asp.net中用户自定义控件 ascx的使用第1张Asp.net中用户自定义控件 ascx的使用第2张Code
Asp.net中用户自定义控件 ascx的使用第3张<table width="100%" border="0" cellpadding="0" cellspacing="0">
Asp.net中用户自定义控件 ascx的使用第3张      
<tr>
Asp.net中用户自定义控件 ascx的使用第3张          
<td>
Asp.net中用户自定义控件 ascx的使用第3张            
<asp:DropDownList id="ddlProvince" runat="server" Width="100px" AutoPostBack="True" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">   
Asp.net中用户自定义控件 ascx的使用第3张            
</asp:DropDownList>

Asp.net中用户自定义控件 ascx的使用第3张            
<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline">
Asp.net中用户自定义控件 ascx的使用第3张                  
<ContentTemplate>
Asp.net中用户自定义控件 ascx的使用第3张                    
<asp:DropDownList id="ddlCity" runat="server" Width="100px" AutoPostBack="True" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">          
Asp.net中用户自定义控件 ascx的使用第3张                    
</asp:DropDownList>

Asp.net中用户自定义控件 ascx的使用第3张                  
</ContentTemplate>
Asp.net中用户自定义控件 ascx的使用第3张                
<Triggers>
Asp.net中用户自定义控件 ascx的使用第3张                    
<asp:AsyncPostBackTrigger ControlID="ddlProvince" EventName="SelectedIndexChanged" />
Asp.net中用户自定义控件 ascx的使用第3张                
</Triggers>
Asp.net中用户自定义控件 ascx的使用第3张              
</asp:UpdatePanel>
Asp.net中用户自定义控件 ascx的使用第3张              
<asp:UpdatePanel ID="UpdatePanel2" runat="server" RenderMode="Inline">
Asp.net中用户自定义控件 ascx的使用第3张                  
<ContentTemplate>
Asp.net中用户自定义控件 ascx的使用第3张                    
<asp:DropDownList id="ddlDistrict" runat="server" Width="100px">            
Asp.net中用户自定义控件 ascx的使用第3张                     
</asp:DropDownList>

Asp.net中用户自定义控件 ascx的使用第3张                  
</ContentTemplate>
Asp.net中用户自定义控件 ascx的使用第3张                  
<Triggers>
Asp.net中用户自定义控件 ascx的使用第3张                      
<asp:AsyncPostBackTrigger ControlID="ddlCity" EventName="SelectedIndexChanged" />
Asp.net中用户自定义控件 ascx的使用第3张                  
</Triggers>
Asp.net中用户自定义控件 ascx的使用第3张              
</asp:UpdatePanel>
Asp.net中用户自定义控件 ascx的使用第3张          
</td>
Asp.net中用户自定义控件 ascx的使用第3张      
</tr>
Asp.net中用户自定义控件 ascx的使用第3张  
</table>
Asp.net中用户自定义控件 ascx的使用第3张
Asp.net中用户自定义控件 ascx的使用第3张

ascx后台代码:

Asp.net中用户自定义控件 ascx的使用第31张Asp.net中用户自定义控件 ascx的使用第32张Code
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,把你所需要传入的参数通过下面的方式给它就可以了,至于出入后你需要怎么处理你就自己操作了,上面有我的简单处理方式。

Asp.net中用户自定义控件 ascx的使用第33张Asp.net中用户自定义控件 ascx的使用第34张Code
  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的使用有个初步简单的了解。有好的方法相互交流,共同进步!

免责声明:文章转载自《Asp.net中用户自定义控件 ascx的使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇----vue中使用高德地图实现搜索地址----mysql设置数据库默认编码和表名不区分大小写下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

DropDownList无刷新级联下拉(固定级联),Jquery获取JOSN数据

1.HTML页面 <td align="left">                                <asp:DropDownList ID="ddlOne" runat="server">                                </asp:DropDownList>      ...

FCKeditor 编辑器的用法转

昨天抛弃了FreeTextBox1.6中文版,改用FCKeditor 2.1.1。FCKeditor不仅加上了FLASH插入功能,而且它的兼容性超强:支持多种浏览器包括IE 5.5+、Firefox 1.0+、Mozilla 1.3、Netscape 7+;无平台限制,在Windows、Mac、Linux下都能运行;可以和多种WEB语言融合包括:ASP.N...

DataGrid控件用法

实现模版列有超连接外观,一点实现打开或者下载的功能。 <ItemTemplate><a href='http://t.zoukankan.com/download.aspx?DocTitle=<%# DataBinder.Eval(Container.DataItem,"DocTitle") %>'> <%# Dat...

ASP.Net超时时间已到解决办法-

解决办法 1.在代码里面,把未关闭的连接关闭 2.扩大共享池,方法如下: 解决方法可以是修改连接池的连接生存期,因为默认值是60秒,即连接从应用程序被释放后可以在池中保存的时间。 具体操作步骤如下: 如果是ODBC的话,则可以在ODBC Data Source Administrator中手动更改,该程序位于“Start”菜单中的“Programs”-&g...

html中#include file的使用方法

有两个文件a.htm和b.htm,在同一文件夹下a.htm内容例如以下 <!-- #include file="b.htm" --> b.htm内容例如以下 今天:雨 31 ℃~26 ℃ <br />明天:雷阵雨 33 ℃~27 ℃ 直接在浏览器中打开a,没有不论什么显示,后来知道,include是SSI(Server Side...

ASP.NET AJAX入门系列:使用ScriptManager控件

ScriptManager控件包括在ASP.NET 2.0 AJAX Extensions中,它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问Web Service,所有需要支持ASP.NET AJAX的ASP.NET页面上有且只能有一个ScriptManager控件。在ScriptManager控件...