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

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

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

vs2005下ascx页面的代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
<table width="100%" border="0" cellpadding="0" cellspacing="0">
      
<tr>
          
<td>
            
<asp:DropDownList id="ddlProvince" runat="server" Width="100px" AutoPostBack="True" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">   
            
</asp:DropDownList>
            
<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline">
                  
<ContentTemplate>
                    
<asp:DropDownList id="ddlCity" runat="server" Width="100px" AutoPostBack="True" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">          
                    
</asp:DropDownList>
                  
</ContentTemplate>
                
<Triggers>
                    
<asp:AsyncPostBackTrigger ControlID="ddlProvince" EventName="SelectedIndexChanged" />
                
</Triggers>
              
</asp:UpdatePanel>
              
<asp:UpdatePanel ID="UpdatePanel2" runat="server" RenderMode="Inline">
                  
<ContentTemplate>
                    
<asp:DropDownList id="ddlDistrict" runat="server" Width="100px">            
                     
</asp:DropDownList>
                  
</ContentTemplate>
                  
<Triggers>
                      
<asp:AsyncPostBackTrigger ControlID="ddlCity" EventName="SelectedIndexChanged" />
                  
</Triggers>
              
</asp:UpdatePanel>
          
</td>
      
</tr>
  
</table>

ascx后台代码:

ContractedBlock.gif ExpandedBlockStart.gif 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,把你所需要传入的参数通过下面的方式给它就可以了,至于出入后你需要怎么处理你就自己操作了,上面有我的简单处理方式。

ContractedBlock.gif ExpandedBlockStart.gif 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的使用有个初步简单的了解。有好的方法相互交流,共同进步!

转载于:https://www.cnblogs.com/chaodongwang/archive/2008/12/12/ascx.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值